One, Collections module
On the basis of the built-in data types (dict, list, set, tuple), the collections module also provides several additional data types: Counter, deque, Defaultdict, Namedtuple, and Ordereddict.
1.namedtuple: Generate a tuple that can access the content of an element by using a name
2.deque: Double-ended queue to quickly append and eject objects from the other side
3.Counter: Counter, mainly used to count
4.OrderedDict: Ordered Dictionary
5.defaultdict: Dictionary with default values
1 #!/usr/bin/python Env2 #_*_coding:utf-8_*_3 4 #1.namedtuple (Focus)5 fromCollectionsImportNamedtuple6Ponit = Namedtuple ('Ponit',['x','y','Z'])7p = ponit (1,2,5)8 Print(p.x)9 Print(P.Y)Ten Print(P.Z) One A #2.deque - " " - when using list to store data, accessing elements by index is very fast, but inserting and deleting elements is very slow because list is linear, and when data is large, insertions and deletions are inefficient the Deque is a two-way list for efficient insert and delete operations, suitable for queues and stacks: - " " - fromCollectionsImportdeque -Q = deque (['a','b','C']) +Q.append ('x') -Q.appendleft ('y') + #print (q) A #deque ([' Y ', ' A ', ' B ', ' C ', ' X ']) at #Deque supports Appendleft () and Popleft () in addition to the list's append () and pop (), which makes it very efficient to add or remove elements to the head - - #3.OrderedDict (Focus) consumes memory - " " - when using Dict, key is unordered. We cannot determine the order of key when we iterate over the dict. - If you want to keep the key in order, you can use Ordereddict: in " " - fromCollectionsImportordereddict toD = Dict ([('a', 1), ('b', 2), ('C', 3)]) + Print(d) - #D Dict's key is unordered. the #{' A ': 1, ' C ': 3, ' B ': 2} *OD = Ordereddict ([('a', 1), ('b', 2), ('C', 3)]) $ Print(OD)Panax Notoginseng #The od ordereddict key is ordered. - #ordereddict (' A ', 1), (' B ', 2), (' C ', 3)]) the + #Note that the Ordereddict key is sorted in the order in which it was inserted, not the key itself . AOD =ordereddict () thend'Z'] = 1 +nd'y'] = 2 -nd'x'] = 3 $Od.keys ()#return in the order of the inserted key $ #[' z ', ' y ', ' x '] - - #4.defaultdict (Focus) (with default dictionary, that is, when this is) the #There is a collection of the following values [11,22,33,44,55,66,77,88,99,90 ...], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key. - #Ordinary code, need to judge this key,vales existence does not existWuyivalues = [11, 22, 33,44,55,66,77,88,99,90] theMy_dict = {} - forValueinchvalues: Wu ifvalue>66: - ifMy_dict.has_key ('K1'): Aboutmy_dict['K1'].append (value) $ Else: -my_dict['K1'] =[value] - Else: - ifMy_dict.has_key ('K2'): Amy_dict['K2'].append (value) + Else: themy_dict['K2'] =[value] - $ #when using Dict, if the referenced key does not exist, the keyerror is thrown. If you want the key to not exist, return a default value, you can use Defaultdict: the #The advanced version will return the default parameters, using the principle of anonymous functions the fromCollectionsImportdefaultdict thevalues = [11, 22, 33,44,55,66,77,88,99,90] theMy_dict =defaultdict (list) - forValueinchvalues: in ifvalue>66: themy_dict['K1'].append (value) the Else: Aboutmy_dict['K2'].append (value) the the #Anonymous Functions the fromCollectionsImportdefaultdict + deffunc (): - return 'N /A' theMy_dict =defaultdict (func)Bayi Print(my_dict['k']) the the #5.Counter - #The purpose of the counter class is to track the number of occurrences of a value. It is an unordered container type, stored in the form of a dictionary key-value pair, where the element is counted as the key and its count as value. The count value can be any Interger (including 0 and negative numbers). The counter class is similar to bags or multisets in other languages. - fromCollectionsImportCounter thec = Counter ('Abcdeabcdabcaba') the Print(c) the #output: Counter ({' A ': 5, ' B ': 4, ' C ': 3, ' d ': 2, ' E ': 1}) the #http://www.cnblogs.com/Eva-J/articles/7291842.html
Python Development Module Basics: Collections Module