Skip to content

[Python] 如何透過 Dict 或 Counter 模組統計數量

Python 是個簡單優雅的語言,可以使用很少的指令來達成我們想要的功能,故也是目前相當多人用於統計分析的程式語言。

今天我想紀錄下在 Python 當中『統計』的方法,以往我一直是用 Dictionary 的資料型態進行統計,但最近被朋友提醒了用 Python 自帶的 Counter 也不錯。

以下就來簡單地討論這兩種不同的方法吧。

如果想看關於 Dictionary 的教學,可以參考我之前寫過的《Python 基本教學 (十一) Tuples, Sets, Dictionary》

若是想要直接看看官方的教學,也許可以參考這裡:https://docs.python.org/3.6/tutorial/datastructures.html#tuples-and-sequences


Dictionary

首先,假設我們有著以下的資料。這是我隨機產生的資料,只有 4 種動物,但是隨機選了 20 次放入 List 當中。

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


我們可以使用 Dict 來統計數量:

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

print(results)



Output:

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

基本上我一直覺得這個寫法還滿快的,不過若是要排序輸出的話,得另外寫個 Bubble Sort 之類的排序。


Counter

Counter 是 Python 裡面自帶的模組,可以快速統計 List 內的元件,很方便的是,連排序也不需要我們多費工夫。

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: