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