There are some of the more useful types in the collections module, which are some of the basic types of extensions that are necessary for one of the modules
Counter (counter)
Can track the number of occurrences of a value, and is arranged from large to small
>>> Import collections>>> C1 = collections. Counter (' ADADWEFFFCVCC ') >>> c1counter ({' C ': 3, ' F ': 3, ' a ': 2, ' d ': 2, ' E ': 1, ' W ': 1, ' V ': 1})
2.OrderedDict (ordered dictionary)
The dictionary types in Python are unordered, sometimes disorderly, and can be ordereddict to generate an ordered dictionary in the order in which they are inserted.
>>> import collections>>> D = dict ([' K1 ', one-to-one), (' K2 ', ' + '), (' K3 ', ' + ')]) >>> d{' K3 ':, ' K2 ': 22, ' K1 ': 11}>>> order_d = collections. Ordereddict ([' K1 ', one-to-one), (' K2 ', ') ', (' K3 ', ')]) >>> order_dordereddict ([' K1 ', one-by-one), (' The [') ', ' + '), (' K2 ', 33)])
3.defaultdict (Default dictionary)
Set a default type for the value in the dictionary, which can be list, tuple, etc.
>>> import collections>>> dic = collections.defaultdict (list) >>> dic[' K1 '].append (1) > >> dicdefaultdict (<type ' list ', {' K1 ': [1]}) >>> dic = {}>>> dic[' k1 '] = []>>> dic [' K1 '].append (1) >>> dic{' K1 ': [1]}
4.namedtuple (can be named tuples)
This is not the same as the normal tuple creation process, the general tuple is to create the object with a class that already exists in Python, then use the object, then create the class, create the object through this class, and then use the object. It is a subclass of a tuple in python that inherits the methods of tuples in Python.
>>> import collections>>> mytuple = collections.namedtuple (' mytuple ', [' X ', ' y ']) >>> new = Mytuple >>> Print newmytuple (x=1, y=2) >>> new.x1>>> new.y2
5.deque (bidirectional queue)
Both can be taken at both ends, can be inserted, greatly improve the efficiency of insertion and removal
>>> import collections>>> D = Collections.deque ([11,22,55,66]) >>> ddeque ([11, 22, 55, 66])
,
This article from "Chu Water June" blog, please be sure to keep this source http://artvary.blog.51cto.com/10506823/1894589
Python Collections Module