import collections
l = [2,3,4,5,6,7,8]
words = ['I']*random.choice(l)+['love']*random.choice(l)+['Python']*random.choice(l)+['becase']*random.choice(l)+["it's"]*random.choice(l)+['funny']*random.choice(l)
ct = collections.Counter(words)
(1)获取单词在words中出现的次数
ct.get('Python') ## 获取'Python'在words中出现的次数,等价于ct['Python']
## 运行结果:
6
(2)## .elements()方法生成一个迭代器,遍历地返回words中出现的词
for ix, i in enumerate(ct.elements()):
if ix > 10:
break
print(ix, i)
## 运行结果:
0 Python
1 Python
2 Python
3 Python
4 Python
5 Python
6 it's
7 it's
8 it's
9 it's
10 it's
(3)# 返回出现频数最高的topn个词与对应频数构成的二元元组所组成的列表
ct.most_common(2)
## 运行结果:
[("it's", 7), ('Python', 6)]