collections.Counter
type can be used to count hashable objects, or used as a multi-collection - a multi-collection is a collection Elements of can appear multiple times 1. The
collections.Counter type is similar to bags
or multisets2
in other programming languages.
(1) Basic usage
counter = collections.Counter(['生物', '印记', '考古学家', '生物', '枣', '印记']) logging.info('counter -> %s', counter) counter.update(['化石', '果实', '枣', '生物']) logging.info('counter -> %s', counter) most = counter.most_common(2) logging.info('most -> %s', most)
Running results:
INFO - counter -> Counter({'生物': 2, '印记': 2, '考古学家': 1, '枣': 1}) INFO - counter -> Counter({'生物': 3, '印记': 2, '枣': 2, '考古学家': 1, '化石': 1, '果实': 1}) INFO - most -> [('生物', 3), ('印记', 2)]
In the sample program, first use collections.Counter() to initialize the counter object. The current number of word occurrences has been calculated in the counter object; collections.Counter()
The input parameter is an iterable object, such as the list here. Then use the update()
method to pass in the new word list. At this time, the counter object will update the counter and perform cumulative calculations. Finally, use the most_common()
method of the counter object to print out the ranking of times. Top 2 word lists.
(2) Set operations
The collections.Counter type also supports set operations.
a = collections.Counter({'老虎': 3, '山羊': 1}) b = collections.Counter({'老虎': 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)
Running result:
INFO - a -> Counter({'老虎': 3, '兔子': 2, '山羊': 1}) INFO - b -> Counter({'山羊': 3, '老虎': 1}) INFO - a+b -> Counter({'老虎': 4, '山羊': 4, '兔子': 2}) INFO - a-b -> Counter({'老虎': 2, '兔子': 2}) INFO - a&b -> Counter({'老虎': 1, '山羊': 1}) INFO - a|b -> Counter({'老虎': 3, '山羊': 3, '兔子': 2})
A and b in the example are both Counter type objects. It also demonstrates that the Counter object can be initialized using key-value pairs;
a b represents the union operation, including all elements;
a-b represents the difference set operation;
a&b represents the intersection operation;
a|b is special, first including all keys, Then compare the maximum value of the corresponding keys in the two objects as the value of the new object. For example, if object a contains 'tiger': 3, object b contains 'tiger': 1, then the final object obtained is 'tiger': 3.
(3) Positive and negative value 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)
Running results:
INFO - +c -> Counter({'x': 1}) INFO - -c -> Counter({'y': 1})
By simply using /-
as the prefix of the Counter type object, positive and negative count filtering can be achieved. This design of Python is very elegant.
Related free learning recommendations: python video tutorial
The above is the detailed content of Understanding Python's collections.Counter type. For more information, please follow other related articles on the PHP Chinese website!