Python Collections

Original please indicate the source: https://www.cnblogs.com/agilestyle/p/12232548.html

 

collections module provides some built-in standard containers can be replaced as Python dict, list, set and selected tuple like.

Counter type

It is a subclass of dict is provided a counting function may be a hash object.

Suppose you want to count the number of duplicate entries in a list of strings appear, as long as the following code to achieve

from collections import Counter

counter = Counter()

for hose into [ ' python ' , ' java ' , ' python ' , ' NodeJS ' , ' python ' , ' NodeJS ' ]:
    counter [just] + = 1

print(counter)

Console Output

Counter({'python': 3, 'nodejs': 2, 'java': 1})

 

Reference

https://docs.python.org/3/library/collections.html

Guess you like

Origin www.cnblogs.com/agilestyle/p/12232548.html