Skip to content

[Python] How to count the quantity through the “Dict” or “Counter” module

Python is a simple and elegant language that can use very few commands to achieve the functions we want, so it is also a programming language used by many people for statistical analysis.

Today I want to record the method of statistics in Python. In the past, I have always used the data type of dictionary for statistics, but recently I was reminded by a friend that it is good to use the Counter module with Python.

Let’s briefly discuss these two different methods below.

If you want to read the tutorial about dictionary: [Python] Tutorial(11) Tuples, Sets, Dictionary

If you want to read the official document: https://docs.python.org/3.6/tutorial/datastructures.html#tuples-and-sequences


Dictionary

First, suppose we have the following information. This is the data I randomly generated. There are only 4 kinds of animals, but I randomly selected them 20 times and put them in the List.

animals = [
    'cat',
    'horse',
    'cat',
    'dog',
    'mouse',
    'cat',
    'cat',
    'mouse',
    'cat',
    'dog',
    'cat',
    'mouse',
    'horse',
    'cat',
    'dog',
    'cat',
    'dog',
    'cat',
    'horse',
    'dog'
]




We can use Dict to count the quantity:

results = {}
for animal in animals:
    results[animal] = results.get(animal, 0) + 1

print(results)



Output:

{'cat': 9, 'horse': 3, 'dog': 5, 'mouse': 3}

Basically, I always think this writing method is quite fast, but if you want to sort the output, you have to write another sort like Bubble Sort.


Counter

Counter is a built-in module in Python, which can quickly count the components in the List. It is very convenient that even sorting does not require us to spend much time.

from collections import Counter

c = Counter(animals)
print(c)
print(c.most_common())



Output:

Counter({'cat': 9, 'dog': 5, 'horse': 3, 'mouse': 3})
[('cat', 9), ('dog', 5), ('horse', 3), ('mouse', 3)]
Tags:
Exit mobile version