Counter
Role: Counter as a container, you can track how many times the same value has been increased
Supports 3 types of initialization
#!/usr/bin/env Pythonimport collectionsprint Collections. Counter ([' A ', ' B ', ' C ', ' A ', ' B ', ' C ']) print collections. Counter ({' A ': 2, ' B ': 2, ' C ': 2}) Print collections. Counter (a=2,b=2,c=2)
Execution results
Counter ({' A ': 2, ' C ': 2, ' B ': 2}) Counter ({' A ': 2, ' C ': 2, ' B ': 2}) Counter ({' A ': 2, ' C ': 2, ' B ': 2})
Constructs an empty counter, populated with the update () method
#!/usr/bin/env pythonimport COLLECTIONSC = collections. Counter () Print cc.update (' aabbcc ') print cc.update ({' A ': 2, ' d ': 2}) Print cc.update (a=4,b=6,c=6,d=6) Print C
Execution Result:
Counter () Counter ({' A ': 2, ' C ': 2, ' B ': 2}) Counter ({' A ': 4, ' C ': 2, ' B ': 2, ' d ': 2}) Counter ({' A ': 8, ' C ': 8, ' B ': 8, ' d ': 8 })
Access Count
Once populated with counter, you can use the dictionary API to get its value
#!/usr/bin/env pythonimport COLLECTIONSC = collections. Counter (' ABCDABCD ') for I in ' ABCDE ': print '%s:%d '% (I, c[i])
Execution Result:
a:2b:2c:2d:2e:0
Note: For unknown elements, counter does not produce keyerror, if a value is not found in the input, the count is 0
The elements () method returns an iterator that generates all the elements known to counter, does not guarantee that the order of the elements is constant, and the elements that count less than or equal to 0 are not included
#!/usr/bin/env pythonimport COLLECTIONSC = collections. Counter (' abdcddlsdf ') c[' x '] = 0print cprint list (c.elements ())
Execution Result:
Counter ({' d ': 4, ' A ': 1, ' C ': 1, ' B ': 1, ' F ': 1, ' l ': 1, ' s ': 1, ' X ': 0} ' [' A ', ' C ', ' B ', ' d ', ' d ', ' d ', ' d ', ' f ', ' l ', ' s ' ]
Use Most_common () to generate a sequence that contains n most commonly encountered input values and their corresponding counts
#!/usr/bin/env pythonimport COLLECTIONSC = collections. Counter () with open ('/usr/share/dict/words ', ' RT ') as F:for line in F:c.update (Line.rstrip (). lower ()) print ' most Common: ' For Letter,count in C.most_common (8): print '%s:%9d '% (letter,count)
Execution Result:
Most common:e: 484673i:382454a:378030n:317017o:312955r:311667s:308445t:284943
Count instances support arithmetic and collection operations to complete aggregation of results
#!/usr/bin/env python
Import Collections
C1 = Collections. Counter (a=1,b=5,c=3,d=7)
C2 = collections. Counter (' alpppdnet ')
print ' C1: ', C1
print ' C2: ', C2
Print C1 + C2
Print C1-C2
Print C1 & C2
Print C1 | C2
Execution Result:
C1:counter ({' d ': 7, ' B ': 5, ' C ': 3, ' a ': 1})
C2:counter ({' P ': 3, ' a ': 1, ' E ': 1, ' d ': 1, ' l ': 1, ' n ': 1, ' t ': 1})
Counter ({' d ': 8, ' B ': 5, ' C ': 3, ' P ': 3, ' a ': 2, ' E ': 1, ' l ': 1, ' n ': 1, ' t ': 1})
Counter ({' d ': 6, ' B ': 5, ' C ': 3})
Counter ({' A ': 1, ' d ': 1})
Counter ({' d ': 7, ' B ': 5, ' C ': 3, ' P ': 3, ' a ': 1, ' E ': 1, ' l ': 1, ' n ': 1, ' t ': 1})
Collections-----Container Data types