【发布时间】:2014-04-20 08:38:58
【问题描述】:
我正在使用此代码来计算文本文件中单词出现的频率:
#!/usr/bin/python
file=open("out1.txt","r+")
wordcount={}
for word in file.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v in wordcount.items():
print k, v
如何按频率数字的降序打印输出?
【问题讨论】:
-
附带说明,不要忘记关闭文件。如果可能,我建议使用
with语句。
标签: python frequency word-count text-analysis