collection模块:

    namedtuple:它是一个函数,是用来创建一个自定义的tuple对象的,并且规定了tuple元素的个数,并可以用属性而不是索引来引用tuple的某个元素。所以我们就可以用它来很方便的定义一种数据类型了,它具备了tuple的不可变类型,又可以根据属性来进行引用,十分的方便。

    第二个元素可以传可迭代对象,也可以传字符串,但是字符串之间要以空格隔开

    元素的个数必须和namedtuple的第二个参数的个数相同。

>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(1, 2)
>>> p.x
1
>>> p.y
2

 

    

 

    deque:实现高效插入和删除操作的双向列表,适合用于队列和栈

        使用list进行存储数据的时候,按索引访问元素很快,但是插入和删除元素就很慢了,因为list内部是线性存储的,当数据量很大的时候,插入和删除的效率就很低了。

>>> from collections import deque
>>> q = deque(['a', 'b', 'c'])
>>> q.append('x')
>>> q.appendleft('y')
>>> q
deque(['y', 'a', 'b', 'c', 'x'])
>>> from collections import deque
>>> q = deque(['a', 'b', 'c'])
>>> q.pop('x')
>>> q.popleft('y')
>>> q
deque(['b'])

 

 

        deque还支持根据索引在任意位置插值:

q.insert(0, 'x')

 

 

 

    defaultdict:当使用dict时,如果引用的key不存在的话,就会抛出KeyError的错误。如果希望key不存在的时候,返回一个默认值,这时候就可以使用defaultdict:

>>> from collections import defaultdict
>>> dd = defaultdict(lambda: 'N/A')
>>> dd['key1'] = 'abc'
>>> dd['key1'] # key1存在
'abc'
>>> dd['key2'] # key2不存在,返回默认值
'N/A'

 

        注意:默认值是调用函数返回的,而函数在创建defaultdict对象的时候传入的(除了在Key不存在时返回默认值,他的其他行为跟dict是一样的)

 

    OrderedDict:保持key的顺序。        

>>> from collections import OrderedDict
>>> d = dict([('a', 1), ('b', 2), ('c', 3)])
>>> d # dict的Key是无序的
{'a': 1, 'c': 3, 'b': 2}
>>> od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> od # OrderedDict的Key是有序的
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

 

        注意:OrederedDict的Key会按照插入的顺序排列,不会按照key本身进行排列。

>>> od = OrderedDict()
>>> od['z'] = 1
>>> od['y'] = 2
>>> od['x'] = 3
>>> od.keys() # 按照插入的Key的顺序返回
['z', 'y', 'x']

 

 

        OrederedDict可以实现FIFO(先进先出)的dict,当容量超出限制时,先删除最早添加的Key。

 

    Counter:就是一个简单的计数器,统计出现的个数。

>>> from collections import Counter
>>> c = Counter()
>>> for ch in 'programming':
...     c[ch] = c[ch] + 1
...
>>> c
Counter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})

 

 

 

time模块:

    三种表现形式:1、时间戳 :表示的是从1970年1月1日00:00:00开始计算的偏移量。一旦运行的话返回的是float类型

           2、格式化时间(用来展示给人看的) 

           3、结构化时间struct_time元组共有9个元素(年、月、日、时、分、秒、一年中第几周、一年中第几天、夏令时)

import time
print(time.time())  # 时间戳

print(time.strftime('%Y-%m-%d'))  # 格式化的时间字符串
print(time.strftime('%Y-%m-%d %H:%M:%S'))
print(time.strftime('%Y-%m-%d %X'))  # %X等价于%H:%M:%S
print(time.strftime('%H:%M'))
print(time.strftime('%Y/%m'))

print(time.localtime())  # 本地区的struct_time

print(time.localtime(time.time()))
res = time.localtime(time.time())
print(time.time())
print(time.mktime(res))  # 将一个struct_time转化为时间戳
print(time.strftime('%Y-%m',time.localtime()))
print(time.strptime(time.strftime('%Y-%m',time.localtime()),'%Y-%m'))
%a    Locale’s abbreviated weekday name.     
%A    Locale’s full weekday name.     
%b    Locale’s abbreviated month name.     
%B    Locale’s full month name.     
%c    Locale’s appropriate date and time representation.     
%d    Day of the month as a decimal number [01,31].     
%H    Hour (24-hour clock) as a decimal number [00,23].     
%I    Hour (12-hour clock) as a decimal number [01,12].     
%j    Day of the year as a decimal number [001,366].     
%m    Month as a decimal number [01,12].     
%M    Minute as a decimal number [00,59].     
%p    Locale’s equivalent of either AM or PM.    (1)
%S    Second as a decimal number [00,61].    (2)
%U    Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0.    (3)
%w    Weekday as a decimal number [0(Sunday),6].     
%W    Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0.    (3)
%x    Locale’s appropriate date representation.     
%X    Locale’s appropriate time representation.     
%y    Year without century as a decimal number [00,99].     
%Y    Year with century as a decimal number.     
%z    Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].     
%Z    Time zone name (no characters if no time zone exists).     
%%    A literal '%' character.
格式化字符串的时间格式

相关文章:

  • 2022-12-23
  • 2022-01-29
  • 2021-08-31
  • 2022-01-03
  • 2022-03-04
  • 2021-07-30
  • 2022-12-23
  • 2021-06-12
猜你喜欢
  • 2021-08-08
  • 2022-12-23
  • 2021-10-26
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案