python list中某个元素出现的次数

from collections import Counter
list = [1,2,3,5,8,9,8,5,2]
result = Counter(list)
print(result)
for key,value in result.items():
    print(key,value)

输出结果:

Counter({2: 2, 5: 2, 8: 2, 1: 1, 3: 1, 9: 1})
1 1
2 2
3 1
5 2
8 2
9 1

猜你喜欢

转载自blog.csdn.net/qq_15256443/article/details/89485729