import collections
words = ["萌萌","萌萌","萌萌","萌萌","爱","赖赖"] print(type(collections.Counter(words)))
先指定一个list,然后调用collections.Counter()函数,将list传入,就可以得到计数。
运行结果如下:
Counter({'萌萌': 4, '爱': 1, '赖赖': 1})
若是要进行循环。将每个汉子以及对应的个数输出。
for w ,c in collections.Counter(words).items():
print("word : {},count:{}".format(w,c))
word : 萌萌,count:4
word : 爱,count:1
word : 赖赖,count:1
最后
以上就是无私微笑最近收集整理的关于collections.Counter 计数器的全部内容,更多相关collections.Counter内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复