作者:Shawn
Python3.7
文档:
https://docs.python.org/3/library/collections.html
collections.deque([iterable[, maxlen]])
>>> from collections import deque
>>> q = deque(range(5), maxlen=5)
>>> q
deque([0, 1, 2, 3, 4], maxlen=5)
>>> q.append(5)
>>> q
deque([1, 2, 3, 4, 5], maxlen=5)
>>> q.rotate(3)
>>> q
deque([3, 4, 5, 1, 2], maxlen=5)
>>> q.appendleft(-1)
>>> q
deque([-1, 3, 4, 5, 1], maxlen=5)
>>> q.extendleft([11,22,33])
>>> q
deque([33, 22, 11, -1, 3], maxlen=5)
collections.Counter([iterable-or-mapping])
以键值对的形式储存,元素为key,计数为value。
创建
>>> c = Counter()
># 创建一个空的Counter类
>
>>> c = Counter('gallahad')
># 从一个可iterable对象(list、tuple、dict、字符串等)创建
>
>>> c = Counter({'a': 4, 'b': 2})
># 从一个字典对象创建
>
>>> c = Counter(a=4, b=2)
># 从一组键值对创建
>>> c = Counter("abcdefgab")
>>> c["a"]
2
>>> c["c"]
1
>>> c["h"]
0
>>> c = Counter('which')
>>> c.update('witch') # 使用另一个iterable对象更新
>>> c['h']
3
>>> d = Counter('watch')
>>> c.update(d) # 使用另一个Counter对象更新
>>> c['h']
4
>>> c = Counter('which')
>>> c.subtract('witch') # 使用另一个iterable对象更新
>>> c['h']
1
>>> d = Counter('watch')
>>> c.subtract(d) # 使用另一个Counter对象更新
>>> c['a']
-1
计数为0不一定表示元素不存在。
删除元素
>>> c = Counter("abcdcba")
>>> c
Counter({'a': 2, 'c': 2, 'b': 2, 'd': 1})
>>> c["b"] = 0
>>> c
Counter({'a': 2, 'c': 2, 'd': 1, 'b': 0})
>>> del c["a"]
>>> c
Counter({'c': 2, 'b': 2, 'd': 1})
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> list(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']
>>> c = Counter('abracadabra')
>>> c.most_common()
[('a', 5), ('r', 2), ('b', 2), ('c', 1), ('d', 1)]
>>> c.most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
>>> c = Counter(a=3, b=1)
>>> d = Counter(a=1, b=2)
>>> c + d # c[x] + d[x]
Counter({'a': 4, 'b': 3})
>>> c - d # subtract(只保留正数计数的元素)
Counter({'a': 2})
>>> c & d # 交集: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
>>> c | d # 并集: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
sum(c.values())
#所有计数的总数
c.clear()
#重置Counter对象,注意不是删除
list(c)
#将c中的键转为列表
set(c)
#将c中的键转为set
dict(c)
#将c中的键值对转为字典
c.items()
#转为(elem, cnt)格式的列表
Counter(dict(list_of_pairs))
#从(elem, cnt)格式的列表转换为Counter类对象
c.most_common()[:-n:-1]
#取出计数最少的n-1个元素
c += Counter()
#移除0和负值
collections.defaultdict([default_factory[, ...]])
>>> from collections import defaultdict
>>> d = defaultdict(list)
>>> d['a']
[]
>>> d['a'].append(1)
>>> d
defaultdict(<class 'list'>, {'a': [1]})
>>> d['a'].append(2)
>>> d['b'].append(4)
>>> d
defaultdict(<class 'list'>, {'a': [1, 2], 'b': [4]})
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
d[k] += 1
>>> sorted(d.items())
[('i', 4), ('m', 1), ('p', 2), ('s', 4)]
collections.OrderedDict([items])
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d
OrderedDict()
>>> d['foo'] = 1
>>> d['bar'] = 2
>>> d['spam'] = 3
>>> d['grok'] = 4
>>> for key in d:
... print(key, d[key])
...
foo 1
bar 2
spam 3
grok 4
collections.namedtuple(typename, field_names, *, rename=False, defaults=None, module=None)
>>> from collections import namedtuple
>>> City = namedtuple('City', 'name country population coordinates')
>>> tokyo = City('Tokyo', 'JP', 36.933, (35.689722, 139.691667))
>>> tokyo
City(name='Tokyo', country='JP', population=36.933, coordinates=(35.689722,
139.691667))
>>> tokyo.population
36.933
>>> tokyo.coordinates
(35.689722, 139.691667)
>>> tokyo[1]
'JP'
collections.ChainMap(*maps)
>>> from collections import ChainMap
>>> a = {'x': 1, 'y': 2}
>>> b = {'y': 3 , 'z': 4}
>>> c = ChainMap(a, b)
>>> c
ChainMap({'x': 1, 'y': 2}, {'y': 3, 'z': 4})
>>> len(c)
3
>>> c.keys()
KeysView(ChainMap({'x': 1, 'y': 2}, {'y': 3, 'z': 4}))
>>> c.values()
ValuesView(ChainMap({'x': 1, 'y': 2}, {'y': 3, 'z': 4}))
>>> c['x']
1
>>> c['y']
2
>>> c['y'] = 22
>>> c['z'] = 9
>>> del c['x']
>>> a
{'y': 22, 'z': 9}
>>> c
ChainMap({'y': 22, 'z': 9}, {'y': 3, 'z': 4})
>>> a['a'] = 4
>>> a
{'y': 22, 'z': 9, 'a': 4}
>>> c
ChainMap({'y': 22, 'z': 9, 'a': 4}, {'y': 3, 'z': 4})
collections.UserDict([initialdata])
collections.UserList([list])
collections.UserString(seq)