The collections.Counter type of Python, brother teaches you how to master the collections.Counter type of python, and use the collections.Counter type of python skillfully.

The collections.Counter type can be used to count hashable objects, or as a multiple set-multiple sets are elements in the set that can appear multiple times.

The collections.Counter type is similar to bags or multisets2 in other programming languages.

#(1) Basic usage

counter = collections.Counter(['Biology','Mark','Archaeologist','Biology','Jujube','Mark'])

logging.info(‘counter -> %s’, counter)

counter.update(['Fossil','Fruit','Jujube','Bio'])

most = counter.most_common(2)

logging.info(‘most -> %s’, most)

operation result:

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. At this time, the current number of word occurrences has been calculated in the counter object; collections.Counter() is inserted into an iterable object, such as the list here. Then use the update() method to pass in the list of new words. 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 top 2 word lists.

#(2) Set operations

The collections.Counter type also supports collection 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)

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})

· Both a and b in the example are 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;

· Ab represents the difference operation;

· A&b means intersection operation;

· A|b is special. First, all the keys are included, and then the maximum value of the corresponding keys in the two objects is compared as the value of the new object. For example, if there is'tiger' in object a: 3 and'tiger' in object b: 1, then the final object will be'tiger': 3.

#(3) Positive and negative count

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)

INFO - +c -> Counter({'x': 1})

INFO - -c -> Counter({'y': 1})

By simply +/- as the prefix of the Counter type object, positive and negative count filtering can be achieved. This design of Python is very elegant.

Guess you like

Origin blog.csdn.net/Python_xiaobang/article/details/112324077