Tutorial on using the Python built-in module collections
This article describes how to use the Python built-in module collections. The sample code is based on Python2.x. For more information, see
Collections is a collection module built in Python and provides many useful collection classes.
Namedtuple
We know that tuple can represent the unchanged set. For example, the two-dimensional coordinates of a point can be expressed:
?
However, when we see (1, 2), it is hard to see that this tuple is used to represent a coordinate.
After defining a class, I made a fuss. At this moment, namedtuple came in handy:
?
1 2 3 4 5 6 7 |
>>> From collections import namedtuple >>> Point = namedtuple ('point', ['x', 'y']) >>> P = Point (1, 2) >>> P. x 1 >>> P. y 2 |
Namedtuple is a function used to create a custom tuple object and specify the number of tuple elements. You can use attributes instead of indexes to reference an element of tuple.
In this way, we can use namedtuple to easily define a data type. It has the immutability of tuple and can be referenced Based on attributes, which is very convenient to use.
You can verify that the created Point object is a subclass of tuple:
?
1 2 3 4 |
>>> Isinstance (p, Point) True >>> Isinstance (p, tuple) True |
Similarly, if you want to use coordinates and radius to represent a circle, you can also use namedtuple to define it:
?
1 2 |
# Namedtuple ('name', [attribute list]): Circle = namedtuple ('circle', ['x', 'y', 'R']) |
Deque
When using list to store data, index-based access elements are fast, but inserting and deleting elements is slow, because list is linear storage, and insertion and deletion efficiency is low when there is a large amount of data.
Deque is a two-way list for efficient insert and delete operations. It is suitable for queue and stack:
?
1 2 3 4 5 6 |
>>> From collections import deque >>> Q = deque (['A', 'B', 'C']) >>> Q. append ('x ') >>> Q. appendleft ('y ') >>> Q Deque (['y', 'A', 'B', 'C', 'x']) |
In addition to list append () and pop (), deque also supports appendleft () and popleft (). This allows you to efficiently add or delete elements to or from the header.
Defaultdict
When using dict, if the referenced Key does not exist, a KeyError is thrown. If you want to return a default value when the key does not exist, you can use defaultdict:
?
1 2 3 4 5 6 7 |
>>> From collections import defaultdict >>> Dd = defaultdict (lambda: 'n'/') >>> Dd ['key1'] = 'abc' >>> Dd ['key1'] # key1 exists 'Abc' >>> Dd ['key2'] # If key2 does not exist, the default value is returned. 'N'/' |
Note that the default value is returned by calling the function, and the function is passed in when the defaultdict object is created.
Besides returning the default value when the Key does not exist, defaultdict performs the same behavior as dict.
OrderedDict
When using dict, keys are unordered. During the iteration of dict, we cannot determine the Key order.
To maintain the order of keys, use OrderedDict:
?
1 2 3 4 5 6 7 |
>>> From collections import OrderedDict >>> D = dict ([('A', 1), ('B', 2), ('C', 3)]) >>> D # the keys of dict are unordered. {'A': 1, 'C': 3, 'B': 2} >>> Od = OrderedDict ([('A', 1), ('B', 2), ('C', 3)]) >>> Od # the keys of OrderedDict are ordered. OrderedDict ([('A', 1), ('B', 2), ('C', 3)]) |
Note that the keys of OrderedDict are arranged in the insert order, not the keys themselves:
?
1 2 3 4 5 6 |
>>> Od = OrderedDict () >>> Od ['Z'] = 1 >>> Od ['y'] = 2 >>> Od ['X'] = 3 >>> Od. keys () # return results in the order of inserted keys ['Z', 'y', 'x'] |
OrderedDict can implement a FIFO (first-in-first-out) dict. When the capacity exceeds the limit, delete the first Key to be added:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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:
?
1 2 3 4 5 6 7 |
>>> 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 a subclass of dict. The preceding results show that the 'g', 'M', And 'r' characters appear twice, and the other characters appear once.
Summary
The collections module provides some useful collection classes that can be selected as needed.