利用字典统计
利用pandas统计
利用Counter类统计
1:方法一
data = [1,2,3,4,2,3,4,2,3,4,5,6,6,4,4,5,6,7,4]
data_dict = {}
for key in data:
data_dict[key] = data_dict.get(key,0) + 1
print("data_dict:",data_dict)
输出结果:
data_dict: {1: 1, 2: 3, 3: 3, 4: 6, 5: 2, 6: 3, 7: 1}
2:方法二
import pandas as pd
data = [1,2,3,4,2,3,4,2,3,4,5,6,6,4,4,5,6,7,4]
result = pd.value_counts(data)
print("result:",result)
输出结果:
result: 4 6
6 3
3 3
2 3
5 2
7 1
1 1
dtype: int64
3:方法三:
from collections import Counter
data = [1,2,3,4,2,3,4,2,3,4,5,6,6,4,4,5,6,7,4]
result = Counter(data)
print("result:",result)
输出结果:
result: Counter({4: 6, 2: 3, 3: 3, 6: 3, 5: 2, 1: 1, 7: 1})