"""
python 計算列表內容出現次數
"""
#方法一: l = ['a','a','b','c','d','b','b','b'] test_dict = {} for i in l: #通過key來計算元素個數 test_dict[i] = test_dict.get(i,0) + 1 print(test_dict)
使用python中的內置模塊
#方法二 l = ['a','a','b','c','d','b','b','b'] from collections import Counter r = Counter(l) print(r)
#遍歷循壞字典
for key,value in test_dict.items():
print("Key: " + key)
print("Value: " + str(value))