Counter函數用來遍歷列表中的所有元素 並將元素出現的次數記錄下來
例如:
1.
alist = [1,1,3,99,88,2,2,2]
from collections import Counter
print(Counter(alist))
#輸出結果為:({2: 3,1: 2,3: 1,99: 1,88: 1})
代表 2出現3次 1出現2次 3 99 88分別出現1次
2.
print(Counter(alist).most_common(1))
#輸出結果為:[(2,3)]
代表 取出出現次數最多的元素
3.
print(Counter(alist).most_common(2)) #輸出結果為:[(2,3),(1,2)]
代表 取出出現次數最多的前兩個元素
