Python:查看列表中不同元素各有多少个

需求:统计列表中,不同元素各有多少个

包:collections
方法:collections.Counter(list)
参数说明:list是待统计的列表,返回结果是一个统计后的"字典"!

实现如下:

from collections import *

x1 = [1, 2, 2, 2, 'c', 'c', 32, 12, 12, 32, 'c', 'b']
print(Counter(x1))

## 结果:
{2: 3, 'c': 3, 32: 2, 12: 2, 1: 1, 'b': 1}

你可能感兴趣的:(Python:查看列表中不同元素各有多少个)