第一种使用标准库提供的collections:
from collections import Counter
import numpy as np
num=1000000
lst = np.random.randint(num / 10, size=num)
# 返回的值是字典格式如{'xx':8,'xxx':9}
res = Counter(lst)
# 输出的是出现次数最后的数据如[('xxx', 8), ('xxx', 5),]
Counter(words).most_common(4)
第二种使用numpy模块(更快)
import numpy as np
num=1000000
lst = np.random.randint(num / 10, size=num)
dict(zip(*np.unique(lst, return_counts=True)))