Collections of commonly used built-in modules in python

Python study notes, special records, share with you, I hope it will be helpful to everyone.

Collections is a built-in collection module of Python, which provides many useful collection classes.

namedtuple

We know that tuple can represent an invariant set. For example, the two-dimensional coordinates of a point can be represented as:

p = (1, 2)

However, seeing (1, 2), it is difficult to see that this tuple is used to represent a coordinate.

Defining a class has made a big fuss. At this time, namedtuple comes in handy:

from collections import namedtuple

Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print p.x
print p.y

operation result:

1
2

Process finished with exit code 0

namedtuple is a function, it is used to create a custom tuple object, and specifies the number of tuple elements, and can use attributes instead of index to refer to an element of the tuple.

In this way, we can easily define a data type with namedtuple, which has the invariance of tuple, and can be referenced according to attributes, which is very convenient to use.

It can be verified that the created Point object is a subclass of tuple:

print isinstance(p, Point)
print isinstance(p, tuple)

operation result:

True
True

Process finished with exit code 0

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

# namedtuple('名称', [属性list]):
Circle = namedtuple('Circle', ['x', 'y', 'r'])

and

When using the list to store data, accessing elements by index is fast, but inserting and deleting elements is very slow, because the list is stored linearly, and when the amount of data is large, the efficiency of inserting and deleting is very low.

deque is a two-way list for efficient insertion and deletion operations, suitable for queues and stacks:

from collections import deque

q = deque(['a', 'b', 'c'])
q.append('x')
q.appendleft('y')
print q

operation result:

deque(['y', 'a', 'b', 'c', 'x'])

Process finished with exit code 0

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

defaultdict

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

from collections import defaultdict

dd = defaultdict(lambda : 'N/A')
dd['key1'] = 'abc'
# key1存在
print dd['key1']
# key2不存在,返回默认值
print dd['key2']

operation result:

abc
N/A

Process finished with exit code 0

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

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

OrderedDict

When using dict, Key is out of order. When iterating over the dict, we cannot determine the order of the keys.

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)])
# dict的Key是无序的
print d
od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
# OrderedDict的Key是有序的
print od

operation result:

{'a': 1, 'c': 3, 'b': 2}
OrderedDict([('a', 1), ('b', 2), ('c', 3)])

Process finished with exit code 0

Note that the Keys of OrderedDict will be arranged in the order of insertion, not the Key itself:

od = OrderedDict()
od['z'] = 1
od['y'] = 2
od['x'] = 3
# 按照插入的Key的顺序返回
print list(od.keys())

operation result:

['z', 'y', 'x']

Process finished with exit code 0

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 = 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)

ChainMap

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

When is the most appropriate time to use ChainMap? For example: applications often need to pass in parameters, parameters can be passed in through the command line, can be passed in through environment variables, and there can also be default parameters. We can use ChainMap to realize the 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 = {
    'color': 'red',
    'user': 'guest'
}

# 构造命令行参数:
parser = argparse.ArgumentParser()
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 }

# 组合成ChainMap:
combined = ChainMap(command_line_args, os.environ, defaults)

# 打印参数:
print 'color=%s' % combined['color']
print 'user=%s' % combined['user']

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

$ python3 use_chainmap.py 
color=red
user=guest

When the command line parameters are passed in, the command line parameters are preferred:

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

The command line parameters and environment variables are passed in at the same time, and the command line parameters have a higher priority:

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

Counter

Counter is a simple counter, for example, counting the number of characters that appear:

from collections import Counter

c = Counter()
for ch in 'programming':
    c[ch] = c[ch] + 1
print c

operation result:

Counter({'g': 2, 'm': 2, 'r': 2, 'a': 1, 'i': 1, 'o': 1, 'n': 1, 'p': 1})

Process finished with exit code 0

Counter is actually a subtype of dict. From the above results, it can be seen that the characters'g','m', and'r' each appear twice, and the other characters each appear once.

Welcome to follow the official account **"Net Development"**, you can receive python test demos and learning resources, let everyone learn python together, and gather all the world's methods, which is convenient for you and me to develop .


Welcome to pay attention to the public account "Web Development"

image

Guess you like

Origin blog.csdn.net/qq_36478920/article/details/103260676