1.collections库-累加器
from collections import Counter
doc = 'Python is an interpreted high-level programming language for general-purpose programming. '
#把doc按空格切割
word_list = doc.split()
#调用Counter()函数对单词进行累加
cc = Counter(word_list)
cc
--->
Counter({'Python': 1,
'an': 1,
'for': 1,
'general-purpose': 1,
'high-level': 1,
'interpreted': 1,
'is': 1,
'language': 1,
'programming': 1,
'programming.': 1})
#迭代输出结果
for k,v in cc.most_common():
print(k,v)
--->
Python 1
is 1
an 1
interpreted 1
high-level 1
programming 1
language 1
for 1
general-purpose 1
programming. 1
网友评论