The following is quoted from: Liao Xuefeng official website https://www.liaoxuefeng.com/wiki/897692888725344/973805065315456
collections Python is a collection of built-in module that provides many useful collections.
namedtuple
We know that tuple
can represent the same collection, for example, two-dimensional coordinates of a point can be expressed as:
>>> p = (1, 2)
But, see (1, 2)
, it is difficult to see that this tuple
is used to represent a coordinate.
The definition of a class and fuss, then, namedtuple
came in handy:
>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y']) >>> p = Point(1, 2) >>> p.x 1 >>> p.y 2
namedtuple
It is a function that is used to create a custom tuple
object, and specifies the tuple
number of elements and attributes can be used instead of an index to refer to tuple
an element.
As a result, we namedtuple
can easily define a data type, it has a tuple invariance, and can be referenced according to the attributes, easy to use.
Can verify the creation of Point
the object is tuple
a subclass:
>>> isinstance(p, Point)
True
>>> isinstance(p, tuple)
True
Similarly, if the use coordinates and the radius of a circle represented, can also be used namedtuple
definitions:
# namedtuple('名称', [属性list]):
Circle = namedtuple('Circle', ['x', 'y', 'r'])
and
Use list
as data is stored, accessed by index elements quickly, but inserting and removing elements is very slow, because list
a linear memory, when a large amount of data, efficiency is very low insertion and deletion.
deque is to achieve efficient insertion and deletion of two-way list, queue and stack suitable for:
>>> from collections import deque
>>> q = deque(['a', 'b', 'c']) >>> q.append('x') >>> q.appendleft('y') >>> q deque(['y', 'a', 'b', 'c', 'x'])
deque
In addition to achieving list of append()
and pop()
, but also support appendleft()
and popleft()
, very efficiently so that you can add or delete elements to the head.
defaultdict
Use dict
, if the referenced Key does not exist, it will be thrown KeyError
. If you want the key does not exist, returns 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'
Note that the default value is returned by the function call, and the function creating defaultdict
passed during object.
In addition to returning the default value in the Key does not exist, defaultdict
other acts with dict
exactly the same.
OrderedDict
Use dict
when, Key is disordered. Out of dict
doing iterations, we can not determine the order of Key.
If you want to keep the order Key, 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 OrderedDict
the Key will be arranged in order of insertion is not itself Sort Key:
>>> od = OrderedDict()
>>> od['z'] = 1
>>> od['y'] = 2 >>> od['x'] = 3 >>> od.keys() # 按照插入的Key的顺序返回 ['z', 'y', 'x']
OrderedDict
Can implement a FIFO (First In First Out) of dict, when capacity exceeds the limit, the oldest delete Key added:
from collections import OrderedDict
class LastUpdatedOrderedDict(OrderedDict): def __init__(self, capacity): super(LastUpdatedOrderedDict, self).__init__() self._capacity = capacity def __setitem__(self, key, value): containsKey = 1 if key in self else 0 if len(self) - containsKey >= self._capacity: last = self.popitem(last=False) print 'remove:', last if containsKey: del self[key] print 'set:', (key, value) else: print 'add:', (key, value) OrderedDict.__setitem__(self, key, value)
Counter
Counter
Is a simple counter, for example, count the number of characters that appear:
>>> 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})
Counter
Is actually dict
a subclass, the above results can be seen, character 'g'
, 'm'
, 'r'
each appears twice, once for each occurrence of other characters.
summary
collections
Module provides a useful set of classes may be selected according to need.