The collections module is a newly added feature after python2.7.3.
1.1. Counter (Counter)
Counter is a supplement to the dictionary type that is used to track the number of values
PS: With all the functions of the Dictionary + own function
>>> Import collections>>> c1=collections. Counter (' asdasdf ') >>> print c1counter ({' A ': 2, ' s ': 2, ' d ': 2, ' F ': 1})
1.2, C.update (d): Add C, D two counters
>>> C = Collections. Counter (' aabc ') >>> ccounter ({' A ': 2, ' C ': 1, ' B ': 1}) >>> d=collections. Counter (' AaB ') >>> dcounter ({' A ': 2, ' B ': 1}) >>> C.update (d) >>> ccounter ({' A ': 4, ' B ': 2, ' C ': 1})
1.3 Clear () empty counter
>>> Ccounter ({' A ': 4, ' B ': 2, ' C ': 1}) >>> c.clear () >>> ccounter ()
2. Ordered dictionary (ordereddict):
Ordereddict is a supplement to the dictionary type, and he remembers the order in which the dictionary elements are added
We know that the dictionary is unordered, the list is ordered, and the Ordereddict method is to save the dictionary when the key is assigned to a list in order, such as List=[k1,k2,k3,...]. Finally, the value of the dictionary is called in the order of key.
O1 = collections. Ordereddict () o1[' k1 '] = 1o1[' K2 '] = 2o1[' K3 '] = 3>>> o1ordereddict ([' K1 ', 1), (' K2 ', 2), (' K3 ', 3)])
3. Default dictionary (defaultdict): Sets a default type for values in the dictionary, which can be a list, a tuple, or a dictionary
For example:
>>> my_dict = collections.defaultdict (list) >>> my_dict[' K1 '].append (1) >>> My_ Dictdefaultdict (<type ' list ';, {' K1 ': [1]})
This is because the default values are set to list, so the Append method can be used later to assign values to the list
Default Dictionary 2: The above example can also be written like this
>>> dic = {}>>> dic[' k1 '] = []>>> dic[' K1 '].append (1) >>> dic{' K1 ': [1]}
This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1821985
Collections Series Function Introduction