Collections is a collection module built into Python that provides many useful collection classes.
1、namedtuple
python provides a lot of very nice basic types, such as the immutable type tuple, which we can easily use to represent a binary vector.
>>> v = (2,3)
We find that although (2,3) represents two coordinates of a vector, it is again difficult to see directly that this tuple is used to represent a coordinate without additional specification.
Defining a class for this purpose is again trivial, and this is where namedtuple comes in handy.
>>> from collections import namedtuple >>> Vector = namedtuple('Vector', ['x', 'y']) >>> v = Vector(2,3) >>> 2 >>> 3
namedtuple is a function that creates a customized tuple object and specifies the number of tuple elements and can refer to an element of the tuple with an attribute instead of an index.
In this way, we can easily define a datatype with namedtuple, which has the invariance of tuple and can be referenced based on attributes, making it very easy to use.
We can verify the type of the Vector object created.
>>> type(v) <class '__main__.Vector'> >>> isinstance(v, Vector) True >>> isinstance(v, tuple) True
Similarly, if a circle is to be represented by coordinates and radius, it can be defined using namedtuple:
>>> Circle = namedtuple('Circle', ['x', 'y', 'r']) # namedtuple('Name', [‘Property List'])
2、deque
In data structures, we know queue and stack are two very important data types, one FIFO and one LIFO. In python, when using list to store data, accessing elements by index is fast, but inserting and deleting elements is slow, because list is linear storage, and insertion and deletion are inefficient when the data is large.
deque is to efficiently implement the insertion and deletion operations of the bidirectional chain table structure, very suitable for the implementation of the queue and the stack of such data structures.
>>> from collections import deque >>> deq = deque([1, 2, 3]) >>> (4) >>> deq deque([1, 2, 3, 4]) >>> (5) >>> deq deque([5, 1, 2, 3, 4]) >>> () 4 >>> () 5 >>> deq deque([1, 2, 3])
In addition to implementing list's append() and pop(), deque also supports appendleft() and popleft(), which makes it very efficient to add or remove elements to or from the head.
3、defaultdict
When using the dict dictionary type, KeyError is thrown if the referenced key does not exist. defaultdict can be used if you want a default value to be returned if the key does not exist.
>>> from collections import defaultdict >>> dd = defaultdict(lambda: 'defaultvalue') >>> dd['key1'] = 'a' >>> dd['key1'] 'a' >>> dd['key2'] # key2 is undefined, returns the default value 'defaultvalue'
Note that the default value is returned by calling the function, which is passed in when the defaultdict object is created.
The behavior of defaultdict is exactly the same as dict except that it returns the default value when the Key does not exist.
4、OrderedDict
When using dict, the key is unordered. When doing iterations on dict, we cannot determine the order of the keys.
But if you want to keep the order of the keys, you can use OrderedDict.
>>> from collections import OrderedDict >>> d = dict([('a', 1), ('b', 2), ('c', 3)]) >>> d # dict's Key is unordered {'a': 1, 'c': 3, 'b': 2} >>> od = OrderedDict([('a', 1), ('b', 2), ('c', 3)]) >>> od # OrderedDict's Keys are ordered OrderedDict([('a', 1), ('b', 2), ('c', 3)])
Note that the key of an OrderedDict is sorted in the order it is inserted, not the key itself.
>>> od = OrderedDict() >>> od['z'] = 1 >>> od['y'] = 2 >>> od['x'] = 3 >>> list(()) # Returns the inserted Keys in the order in which they were inserted ['z', 'y', 'x']
OrderedDict can implement a FIFO (first in first out) dict, when the capacity exceeds the limit, the earliest added key is deleted first.
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 = (last=False) print('remove:', last) if containsKey: del self[key] print('set:', (key, value)) else: print('add:', (key, value)) OrderedDict.__setitem__(self, key, value)
5、ChainMap
ChainMap can string together a set of dict and form a logical dict. ChainMap itself is also a dict, but when looking up, it will be in accordance with the order of the internal dict in order to find.
When is it most appropriate to use ChainMap? As an example: applications often need to pass parameters, parameters can be passed through the command line, can be passed through the environment variables, there can also be default parameters. We can use ChainMap to realize the priority of parameter lookup, that is, first check the command line parameters, if there is no input, and then check the environment variables, if there is no, then use the default parameters.
The following code demonstrates how to find the parameters user and color.
from collections import ChainMap import os, argparse # Construct default parameters. defaults = { 'color': 'red', 'user': 'guest' } # Constructing command line arguments. parser = () parser.add_argument('-u', '--user') parser.add_argument('-c', '--color') namespace = parser.parse_args() command_line_args = { k: v for k, v in vars(namespace).items() if v } # Combine into ChainMap. combined = ChainMap(command_line_args, , defaults) # Print parameters. print('color=%s' % combined['color']) print('user=%s' % combined['user'])
When there are no parameters, the default parameters are printed:
$ python3 use_chainmap.py color=red user=guest
When command line arguments are passed in, the command line arguments are preferred:
$ python3 use_chainmap.py -u bob color=red user=bob
Pass in both command line arguments and environment variables, with command line arguments taking precedence:
$ user=admin color=green python3 use_chainmap.py -u bob color=green user=bob
6、Counter
Counter is a simple counter that counts, for example, the number of occurrences of a character:
from collections import Counter >>> s = 'abbcccdddd' >>> Counter(s) Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})
Counter is actually a subclass of dict.
7. Summary
The collections module provides a number of useful collection classes that can be selected as needed.
The above is a small introduction to the python built-in module collections, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. Here also thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!