您的位置:首页 > 编程语言 > Python开发

统计文章出现最多的10个词语--python实现

2014-08-09 21:17 387 查看
f = open("c:\\emma.txt");
count = {}
for line in f:
line = line.strip()
words = line.split()
for word in words:
if word in count:
count[word] +=1
else:
count[word] = 1

word_freq = []
#遍历字典转换为元组
for word,freq in count.items():
word_freq.append((freq,word));
word_freq.sort(reverse = True);
#遍历前十个输出
for word,freq in word_freq[:10]:
print word,freq
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: