The collections.Counter type can be used to count objects that can be hashed, or as multiple collections — where the elements of a collection can appear more than one time.
The collections.Counter type is similar to Bags or Multisets2 in other programming languages.
(1) Basic usage
Counter = collections. Counter ([' biological ', 'mark', 'the archaeologists',' biological ', 'date', 'mark']) logging. The info (' counter - > % s', Counter) counter. Update ([' fossil ', 'fruit', 'date', 'biological']) logging. The info (' counter - > % s', counter) most = counter.most_common(2) logging.info('most -> %s', most)Copy the code
Running results:
The INFO counter - > counter ({' biological ': 2,' mark ': 2, "archaeologists" : 1,' dates' : 1}) INFO counter - > counter ({' biological ': 3,' mark ': 2,' dates: 2, "archaeologists" : 1, "fossil" : 1, "fruit" : 1}) most INFO - - > [(' biological ', 3), (' mark ', 2)]Copy the code
In the sample program, the Counter object is first initialized with collections.counter (), at which point the current occurrences of words have been counted in the Counter object; Collections.counter () takes iterable objects, such as the list here. The update() method is then used to pass in the list of new words, at which point the counter object updates the counter and adds up. Finally, use the most_common() method of the counter object to print out a list of the top two words.
(2) Set operation
The collections.Counter type also supports collection operations.
A = collections.counter ({' tiger ': 3, 'sheep ': 1}) b = collections.counter ({' tiger ': 1,' sheep ': 1}) 3}) logging.info('a -> %s', a) logging.info('b -> %s', b) logging.info('a+b -> %s', a + b) logging.info('a-b -> %s', a - b) logging.info('a&b -> %s', a & b) logging.info('a|b -> %s', a | b)Copy the code
Running results:
INFO - a - > Counter ({' tigers' : 3, "rabbit" : 2, 'goat: 1}) INFO - b - > Counter ({' goat: 3, "tiger" : 1}) INFO - a + b - > Counter ({' tigers' : 4, 4 'goat:,' rabbit ': 2}) INFO - a - b - > Counter ({" tiger ": 2,' rabbit ': 2}) INFO - kathi Jones from a&b - > Counter ({" tiger" : 1,' goats' : 1}) INFO - a | b - > Counter ({' tigers' : 3, 'goat: 3,' rabbit ': 2})Copy the code
- Both a and B in the example are objects of type Counter. It is also shown that the Counter object can be initialized using key-value pairs;
- A +b represents the union operation, containing all elements;
- A-b denotes difference set operation;
- A&b stands for intersection operation;
- A | b is more special, first to encompass all of the key, and then compare the two objects the maximum value of the corresponding key, as a new object of value. For example, if a has’ tiger ‘: 3 and B has’ tiger ‘: 1, the resulting object is’ tiger ‘: 3.
(3) Positive and negative counting
Counters in the Counter type also support negative values.
c = collections.Counter(x=1, y=-1)
logging.info('+c -> %s', +c)
logging.info('-c -> %s', -c)
Copy the code
Running results:
INFO - +c -> Counter({'x': 1})
INFO - -c -> Counter({'y': 1})
Copy the code
You can implement positive and negative count filtering by simply prefixing objects of type Counter with +/-. This is an elegant design of Python.
[1] Luciano Ramalho (author), An Dao, Wu Ke (translator). Smooth Python. Posts and Telecommunications Press, 2017:149-150. [2] Class Colles.counter.