然后怎么输出出现次数前十的单词??[face]emoji:010.png[/face][face]emoji:011.png[/face]



1 统计单词出现次数:
def count_word():
count = {}
keys = []
with open('test.txt') as f:
lines = f.readlines()
for line in lines:
first_word = line.split(' ')[0]
if first_word not in keys:
keys.append(first_word)
count = count.fromkeys(keys,0) #fromkeys方法不会直接修改原字典,它会返回一个新字典,所以如果要使用心字典必须先赋值
print(count)
for line in lines:
first_word = line.split(' ')[0]
count[first_word] +=1
print(count)
2 前10出现的单词:
import re
from collections import Counter
with open('1.txt', 'r', ) as f:
words = f.read() # 将文件的内容全部读取成一个字符串
count = Counter(re.split(r"\W+", words)) # 以单词为分隔
result = count.most_common(10) # 统计最常使用的前10个
print(result)