''''
This module implements specialized container datatypes providing
alternatives to Python's general purpose built-in containers, dict,
list, set, and tuple.
* namedtuple factory function for creating tuple subclasses with named fields
* deque list-like container with fast appends and pops on either end
* ChainMap dict-like class for creating a single view of multiple mappings
* Counter dict subclass for counting hashable objects
* OrderedDict dict subclass that remembers the order entries were added
* defaultdict dict subclass that calls a factory function to supply missing values
* UserDict wrapper around dictionary objects for easier dict subclassing
* UserList wrapper around list objects for easier list subclassing
* UserString wrapper around string objects for easier string subclassing
''''
namedtuple('名称', [属性list]):
举个例子形象说明:
tuple元组可以表示不变集合,如原点的二维坐标可以表示成:a = (0, 0)
为了使我们看到(0,0)能知道它是一个坐标,我们可以用namedtuple:
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(0, 0)
namedtuple是一个函数,它用来创建一个自定义的tuple对象,并且规定了tuple元素的个数,同时可以用属性引用tuple的某个元素。
通过isinstance来验证创建的对象:
print(isinstance(p, Point))
print(isinstance(p, tuple))
输出结果均为true。
list的索引访问速度很快,但因为list是线性存储,当数据量大的时候,插入和删除效率就变得很低,这时我们可以使用deque。deque是双向列表,适合用于队列和栈:
from collections import deque
q = deque(['a', 'b', 'c'])
q.append('x')
print(q)
q.appendleft('y')
print(q)
q.pop()
print(q)
q.popleft()
print(q)
deque([‘a’, ‘b’, ‘c’, ‘x’])
deque([‘y’, ‘a’, ‘b’, ‘c’, ‘x’])
deque([‘y’, ‘a’, ‘b’, ‘c’])
deque([‘a’, ‘b’, ‘c’])
但deque和list不同
print(isinstance(q,list))
False
使用dict时,如果引用的Key不存在,就会返回KeyError。而defaultdict的key不存在时,返回一个默认值:
from collections import defaultdict
dd = defaultdict(lambda: 'N/A')
dd['key1'] = 'abc'
print(dd['key1'])# key1存在
print(dd['key2'])# key2不存在,返回默认值
abc
N/A
除了在Key不存在时返回默认值,defaultdict的其他行为跟dict是完全一样的。
print(isinstance(dd,dict))
True
使用dict时,Key是无序的。在对dict做迭代时,我们无法确定Key的顺序。
如果要保持Key的顺序,可以用OrderedDict:
from collections import OrderedDict
d = dict([('a', 1), ('b', 2), ('c', 3)])
print(d)
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
print(od)
{‘a’: 1, ‘b’: 2, ‘c’: 3}
OrderedDict([(‘a’, 1), (‘b’, 2), (‘c’, 3)])
注意,OrderedDict的Key会按照插入的顺序排列,不是Key本身排序:
from collections import OrderedDict
od = OrderedDict()
od['z'] = 1
od['y'] = 2
od['x'] = 3
print(od.keys())
print(od)
odict_keys([‘z’, ‘y’, ‘x’])
OrderedDict([(‘z’, 1), (‘y’, 2), (‘x’, 3)])
Counter是一个简单的计数器,例如,统计字符出现的个数:
from collections import Counter
c = Counter()
for ch in 'programming':
c[ch] = c[ch] + 1
print(c)
Counter({‘r’: 2, ‘g’: 2, ‘m’: 2, ‘p’: 1, ‘o’: 1, ‘a’: 1, ‘i’: 1, ‘n’: 1})
Counter实际上也是dict的一个子类,上面的结果可以看出,字符’g’、‘m’、'r’各出现了两次,其他字符各出现了一次。
print(isinstance(c,dict))
True
其他功能
from collections import Counter
c = Counter()
for ch in 'programming':
c[ch] = c[ch] + 1
print(c['a']) # 默认将计数器中所有的字符认为一个键,然后统计键出现的次数,即键值。如果键不存在则返回0
print(c.__missing__(5)) # 对于不存在的元素,返回计数器为0
print(c.most_common(2)) # 获取出现次数的前2个字母排名
print(sorted(c.elements())) # 返回计数器中的所有元素,并且按照ascii码进行排序,个数小于1的元素不被包含。
c.update('aa') # 更新counter,增加a
print(c['a']) # 更新后a的个数
c.subtract('a') # 更新counter,减少a
print(c['a']) #更新后a的个数
1
0
[(‘r’, 2), (‘g’, 2)]
[‘a’, ‘g’, ‘g’, ‘i’, ‘m’, ‘m’, ‘n’, ‘o’, ‘p’, ‘r’, ‘r’]
3
2