利用字典统计
利用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)
12345
输出结果:
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)
1234
输出结果:
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)
1234
输出结果:
result: Counter({4: 6, 2: 3, 3: 3, 6: 3, 5: 2, 1: 1, 7: 1})

原文链接:https://blog.csdn.net/qq_41911048/article/details/89227758

 posted on 2020-01-02 16:36  xibuhaohao  阅读(21413)  评论(0编辑  收藏  举报