collections模块(集合模块)¶
小提示:collections模块属于Python内置模块
Counter( args )¶
功能
统计字符串/列表/元组中不同元素的个数
参数
·args:要统计的字符串/列表/元组
返回值
返回一个统计对象,对象中数据是字典格式,其中键是不同的元素,值是该元素的个数,可以像操作字典一样操作这个统计对象
例子
import collections
res = collections.Counter('hello world')
print(res)
res2 = collections.Counter(['A', 'B', 'C', 'A', 'A', 'C'])
print(res2)
res3 = collections.Counter(('篮球', '足球', '篮球', '排球', '足球', '篮球'))
print(res3)