获得列表个数的统计,并获得个数最多的N个数字
标签(空格分隔): python
获得列表个数的统计,并获得个数最多的N个数字
import collections # 导入包
list = ["he","he","he","he","he","she","she","she","she","them","them","them","you","us","us"]
print("打印结果",collections.Counter(list))
print("转化成字典形式",dict(collections.Counter(list)))
print("打印结果中频率最高的三个",collections.Counter(list).most_common(3))
print("打印结果中的键,可以借此来去重",collections.Counter(list).keys())
输出结果
打印结果 Counter({‘he’: 5, ‘she’: 4, ‘them’: 3, ‘us’: 2, ‘you’: 1})
转化成字典形式 {‘he’: 5, ‘she’: 4, ‘them’: 3, ‘you’: 1, ‘us’: 2}
打印结果中频率最高的三个 [(‘he’, 5), (‘she’, 4), (‘them’, 3)]
打印结果中的键,可以借此来去重 dict_keys([‘he’, ‘she’, ‘them’, ‘you’, ‘us’])