Collections Module
On the basis of the built-in data types (dict, list, set, tuple), the collections module also provides several additional data types: Counter, deque, Defaultdict, Namedtuple, and Ordereddict.
1.namedtuple: Generate a tuple that can access the content of an element by using a name
2.deque: Double-ended queue to quickly append and eject objects from the other side
3.Counter: Counter, mainly used to count
4.OrderedDict: Ordered Dictionary
5.defaultdict: Dictionary with default values
Namedtuple
We know that a tuple can represent an immutable collection, for example, a two-dimensional coordinate of a point can be expressed as:
>>> p = (1, 2)
However, it is difficult to see (1, 2) that this tuple is used to represent a coordinate.
At this point, Namedtuple came in handy:
>>> from collections import namedtuple>>> Point = namedtuple(‘Point‘, [‘x‘, ‘y‘])>>> p = Point(1, 2)>>> p.x1>>> p.y2
Similarly, if you want to represent a circle with coordinates and radii, you can also define it with Namedtuple:
#namedtuple(‘名称‘, [属性list]):Circle = namedtuple(‘Circle‘, [‘x‘, ‘y‘, ‘r‘])
Deque
When using the list to store data, accessing the elements by index is fast, but inserting and deleting elements is slow because list is linear and the data is large, and insertions and deletions are inefficient.
Deque is a two-way list for efficient insert and delete operations, suitable for queues and stacks:
>>> from collections import deque>>> q = deque([‘a‘, ‘b‘, ‘c‘])>>> q.append(‘x‘)>>> q.appendleft(‘y‘)>>> qdeque([‘y‘, ‘a‘, ‘b‘, ‘c‘, ‘x‘])
In addition to implementing the list's Append () and pop (), deque supports Appendleft () and Popleft (), which allows you to add or remove elements to the head very efficiently.
Ordereddict
When using Dict, key is unordered. We cannot determine the order of key when we iterate over the dict.
If you want to keep the key in order, you can use Ordereddict:
>>> 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)])
Note that the Ordereddict keys are sorted in the order in which they were inserted, not the key itself:
>>> od = OrderedDict()>>> od[‘z‘] = 1>>> od[‘y‘] = 2>>> od[‘x‘] = 3>>> od.keys() # 按照插入的Key的顺序返回[‘z‘, ‘y‘, ‘x‘]
Defaultdict
There is a collection of the following values [11,22,33,44,55,66,77,88,99,90 ...], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key.
That is: {' K1 ': greater Than, ' K2 ': Less than 66}
Python Native Dictionary workaround
values = [11, 22, 33,44,55,66,77,88,99,90]my_dict = {}for value in values: if value>66: if my_dict.has_key(‘k1‘): my_dict[‘k1‘].append(value) else: my_dict[‘k1‘] = [value] else: if my_dict.has_key(‘k2‘): my_dict[‘k2‘].append(value) else: my_dict[‘k2‘] = [value]
Defaultdict method
from collections import defaultdictvalues = [11, 22, 33,44,55,66,77,88,99,90]my_dict = defaultdict(list)for value in values: if value>66: my_dict[‘k1‘].append(value) else: my_dict[‘k2‘].append(value)
When using Dict, if the referenced key does not exist, the keyerror is thrown. If you want the key to not exist, return a default value, you can use Defaultdict:
>>> from collections import defaultdict>>> dd = defaultdict(lambda: ‘N/A‘)>>> dd[‘key1‘] = ‘abc‘>>> dd[‘key1‘] # key1存在‘abc‘>>> dd[‘key2‘] # key2不存在,返回默认值‘N/A‘
Counter
The purpose of the counter class is to track the number of occurrences of a value. It is an unordered container type, stored in the form of a dictionary key-value pair, where the element is counted as the key and its count as value. The count value can be any Interger (including 0 and negative numbers). The counter class is similar to bags or multisets in other languages.
c = Counter(‘abcdeabcdabcaba‘)print c输出:Counter({‘a‘: 5, ‘b‘: 4, ‘c‘: 3, ‘d‘: 2, ‘e‘: 1})
Common Module Collections