One, counter (counter)
Counter is a supplement to the dictionary type that is used to track the number of occurrences of a value, which integrates all the functions of the parent class Dict.
ImportCollectionsobj= Collections. Counter ('aaabbbcccddd')#counter class under the collections filePrint(obj)#return in dictionary formret= Obj.most_common (4)Print(ret)#the top 4 most forKinchObj.elements ():#value of element native Print(k)#aaabbbcccddd forKvinchObj.items ():#The key of obj is a character, and the value is the number of occurrences, the dictionary that consists of Print(K,V)#a 3 B 3 c 3 d 3 #Dict's Loop method has items () keys () VALUES () #counter (dict) items () keys () values () elements ()obj = collections. Counter ([' One',' A',' -',' A'])Print(obj) obj.update (['Eric',' One',' One'])Print(obj)
Counter
Second, ordered dictionary (ordereddict)
Ordereddict is a supplement to the dictionary type and adds an orderly function on the basis of Dict.
1 #dictionaries and lists are combined into an ordered dictionary2 #dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}3 #li = List ([' K1 ', ' K2 '])4 #5 #For I in Li:6 #print (Dic[i])7 ImportCollections8 9DIC =collections. Ordereddict ()Tendic['K1'] ='v1' Onedic['K2'] ='v2' Adic['K3'] ='v3' - Print(DIC)#ordereddict ([' K1 ', ' v1 '), (' K2 ', ' V2 '), (' K3 ', ' v3 ')]) - theDic.move_to_end ('K1')#put the K1 and its value at the end - Print(DIC)#ordereddict ([' K2 ', ' V2 '), (' K3 ', ' V3 '), (' K1 ', ' v1 ')]) - -Dic.popitem ()#remove the stack from the last start. + Print(DIC)#ordereddict ([' K2 ', ' V2 '), (' K3 ', ' v3 ')]) -ret = Dic.pop ('K2') + Print(DIC)#ordereddict ([(' K3 ', ' v3 ')]) A Print(ret)#v2 at -Dic.setdefault ('K4', 666)#dic[' K4 ' = none when parameters are not supplied - Print(DIC)#ordereddict ([' K3 ', ' V3 '), (' K4 ', 666)]) - -Dic.update ({'K1':'v111','K10':'V10'})#Original, modify the original value, no, add in - Print(DIC)#ordereddict ([' K3 ', ' V3 '), (' K4 ', 666), (' K10 ', ' V10 '), (' K1 ', ' v111 ')])
ordereddict
Three, default dictionary (defaultdict)
Defaultdict is a supplement to the type of dictionary, which defaults to a type for the value of the dictionary.
1 ImportCollections2 3DIC = collections.defaultdict (list)#The value is List4dic['K1'].append ('Alex')5 Print(DIC)#defaultdict (<class ' list ', {' K1 ': [' Alex ']})6 7 8 #has the following values set9 #[11,22,33,44,55,66,77,88,99,90]Ten #save all values greater than 66 to the first key in the dictionary, One #saves a value less than 66 to the value of the second key. A #i.e.: {' K1 ': greater Than, ' K2 ': less than or equal to - #purpose: {' K1 ': [66,77,88,99,90], ' K2 ': [11,22,33,44,55]} - the #The first method: -DIC = {} -All_list = [11,22,33,44,55,66,77,88,99,90] - forIinchall_list: + ifI > 66: - if 'K1' inchDic.keys (): +dic['K1'].append (i) A Else: atdic['K1'] =[I,] - Else: - if 'K2' inchDic.keys (): -dic['K2'].append (i) - Else: -dic['K2'] =[I,] in - #In contrast, even if we don't have to initialize a list to #improved as follows +DIC =collections.defaultdict (list) -All_list = [11,22,33,44,55,66,77,88,99,90] the forIinchall_list: * ifI > 66: $dic['K1'].append (i)Panax Notoginseng Else: -dic['K2'].append (i)
defaultdict
Four, can be named tuple (namedtuple)
Depending on nametuple, you can create a type that contains all the functions of a tuple and other features.
1 #T = (one, one, one, one)2 #name adddress gender Email similar to coordinates3 #t.name equivalent to t[0]4 5 ImportCollections6 7 #create a class that works like Defaultdict8Mytupleclass = Collections.namedtuple ('Mytupleclass',['x','y','Z'])9 Print(Help (Mytupleclass))Tenobj = Mytupleclass (11,22,33) One Print(obj.x) A Print(OBJ.Y) - Print(OBJ.Z)
Namedtuple
V. Queues (deque & queue. Queue)bidirectional queue: Deque
1 ImportCollections2 3 #bidirectional Queue4D =Collections.deque ()5D.append ('1')6D.append ('Ten')7D.appendleft ('1')8 Print(d)#deque ([' 1 ', ' 1 ', ' Ten '])9D.extend (['yy','UU','ZZ','xx'])Ten Print(d)#deque ([' 1 ', ' 1 ', ' ten ', ' yy ', ' uu ', ' zz ', ' xx ']) OneD.rotate (5)#To move several loops A Print(d)#deque ([' Ten ', ' yy ', ' uu ', ' zz ', ' xx ', ' 1 ', ' 1 '])
deque One-way queues: queue. Queue
1 #one-way queues2 ImportQueue3 4Q =queue. Queue ()5Q.put ('123')6Q.put ('678')7 Print(Q.qsize ())#28 Print(Q.get ())#123 can only be taken in the order of the class
queue. Queue
7.Python Self-study Road: Collections series