Home > Backend Development > Python Tutorial > Introduction to python built-in module collections

Introduction to python built-in module collections

angryTom
Release: 2019-11-30 15:45:21
forward
4790 people have browsed it

Introduction to python built-in module collections

Introduction to Python’s built-in module collections

Collections is a built-in collection module in Python that provides many useful Collection class.

1. namedtuple

Python provides many very useful basic types, such as the immutable type tuple, which we can easily use to represent a binary vector .

Recommended learning: Python video tutorial

>>> v = (2,3)
Copy after login

We found that although (2,3) represents the two coordinates of a vector, if there is no additional explanation , and it is difficult to directly see that this tuple is used to represent a coordinate.

Defining a class for this is a big deal. At this time, namedtuple comes in handy.

>>> from collections import namedtuple
>>> Vector = namedtuple('Vector', ['x', 'y'])
>>> v = Vector(2,3)
>>> v.x
2
>>> v.y
3
Copy after login

namedtuple is a function that creates a custom tuple object and specifies the number of tuple elements, and can use attributes instead of indexes to reference an element of the tuple.

In this way, we can use namedtuple to easily define a data type, which has the invariance of tuple and can be referenced based on attributes, making it very convenient to use.

We can verify the type of the created Vector object.

>>> type(v)
<class &#39;__main__.Vector&#39;>
>>> isinstance(v, Vector)
True
>>> isinstance(v, tuple)
True
Copy after login

Similarly, if you want to use coordinates and radius to represent a circle, you can also use namedtuple to define:

>>> Circle = namedtuple(&#39;Circle&#39;, [&#39;x&#39;, &#39;y&#39;, &#39;r&#39;])
# namedtuple(&#39;名称&#39;, [‘属性列表’])
Copy after login

2, deque

In the data In the structure, we know that queue and stack are two very important data types, one is first in first out and the other is last in first out. In python, when using a list to store data, accessing elements by index is very fast, but inserting and deleting elements is very slow, because the list is linear storage, and when the amount of data is large, the efficiency of insertion and deletion is very low.

Deque is a doubly linked list structure for efficient implementation of insertion and deletion operations. It is very suitable for implementing data structures such as queues and stacks.

>>> from collections import deque
>>> deq = deque([1, 2, 3])
>>> deq.append(4)
>>> deq
deque([1, 2, 3, 4])
>>> deq.appendleft(5)
>>> deq
deque([5, 1, 2, 3, 4])
>>> deq.pop()
4
>>> deq.popleft()
5
>>> deq
deque([1, 2, 3])
Copy after login

In addition to implementing append() and pop() of list, deque also supports appendleft() and popleft(), so that you can add or delete elements to the head very efficiently.

3. defaultdict

When using the dict dictionary type, if the referenced key does not exist, KeyError will be thrown. If you want a default value to be returned when the Key does not exist, you can use defaultdict.

>>> from collections import defaultdict
>>> dd = defaultdict(lambda: &#39;defaultvalue&#39;)
>>> dd[&#39;key1&#39;] = &#39;a&#39;
>>> dd[&#39;key1&#39;]
&#39;a&#39;
>>> dd[&#39;key2&#39;] # key2未定义,返回默认值
&#39;defaultvalue&#39;
Copy after login

Note that the default value is returned by calling the function, and the function is passed in when creating the defaultdict object.

Except for returning the default value when the Key does not exist, the other behaviors of defaultdict are exactly the same as dict.

4. OrderedDict

When using dict, the keys are unordered. When iterating over dict, we cannot determine the order of keys.

But if you want to keep the order of keys, you can use OrderedDict.

>>> from collections import OrderedDict
>>> d = dict([(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3)])
>>> d # dict的Key是无序的
{&#39;a&#39;: 1, &#39;c&#39;: 3, &#39;b&#39;: 2}
>>> od = OrderedDict([(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3)])
>>> od # OrderedDict的Key是有序的
OrderedDict([(&#39;a&#39;, 1), (&#39;b&#39;, 2), (&#39;c&#39;, 3)])
Copy after login

Note that the keys of OrderedDict will be arranged in the order of insertion, not the key itself.

>>> od = OrderedDict()
>>> od[&#39;z&#39;] = 1
>>> od[&#39;y&#39;] = 2
>>> od[&#39;x&#39;] = 3
>>> list(od.keys()) # 按照插入的Key的顺序返回
[&#39;z&#39;, &#39;y&#39;, &#39;x&#39;]
Copy after login

OrderedDict can implement a FIFO (first in, first out) dict. When the capacity exceeds the limit, first Delete the earliest added key.

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(&#39;remove:&#39;, last)
        if containsKey:
            del self[key]
            print(&#39;set:&#39;, (key, value))
        else:
            print(&#39;add:&#39;, (key, value))
        OrderedDict.__setitem__(self, key, value)
Copy after login

5. ChainMap

ChainMap can string together a set of dicts to form a logical dict. ChainMap itself is also a dict, but when searching, it will search the internal dicts in order.

When is it most appropriate to use ChainMap? For example: Applications often need to pass in parameters. Parameters can be passed in through the command line, passed in through environment variables, and can also have default parameters. We can use ChainMap to implement priority search of parameters, that is, first check the command line parameters, if not passed in, then check the environment variables, if not, use the default parameters.

The following code demonstrates how to find the two parameters user and color.

from collections import ChainMap
import os, argparse
# 构造缺省参数:
defaults = {
    &#39;color&#39;: &#39;red&#39;,
    &#39;user&#39;: &#39;guest&#39;
}
# 构造命令行参数:
parser = argparse.ArgumentParser()
parser.add_argument(&#39;-u&#39;, &#39;--user&#39;)
parser.add_argument(&#39;-c&#39;, &#39;--color&#39;)
namespace = parser.parse_args()
command_line_args = { k: v for k, v in vars(namespace).items() if v }
# 组合成ChainMap:
combined = ChainMap(command_line_args, os.environ, defaults)
# 打印参数:
print(&#39;color=%s&#39; % combined[&#39;color&#39;])
print(&#39;user=%s&#39; % combined[&#39;user&#39;])
Copy after login

When there are no parameters, print out the default parameters:

$ python3 use_chainmap.py 
color=red
user=guest
Copy after login

When the command line parameters are passed in, the command line parameters are used first:

$ python3 use_chainmap.py -u bob
color=red
user=bob
Copy after login

At the same time, the command line is passed in Parameters and environment variables, command line parameters have higher priority:

$ user=admin color=green python3 use_chainmap.py -u bob
color=green
user=bob
Copy after login

6, Counter

Counter is a simple counter, for example, counting the number of occurrences of characters Count:

from collections import Counter
>>> s = &#39;abbcccdddd&#39;
>>> Counter(s)
Counter({&#39;d&#39;: 4, &#39;c&#39;: 3, &#39;b&#39;: 2, &#39;a&#39;: 1})
Copy after login

Counter is actually a subclass of dict.

7. Summary

The collections module provides some useful collection classes, which can be selected as needed.


The above is the detailed content of Introduction to python built-in module collections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template