所以我有下面的代码来计算文本文件中的单词数。我希望通过对出现次数最少的单词出现次数最多的单词对其输出进行排序。如何实现这一目标?
ally = open("alice.txt", "r")
wordcount={}
for word in ally.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v, in wordcount.items():
print(k,v)
答案 0 :(得分:2)
只需使用Counter即可。它将缩短您的代码并为您提供所需的订单。
从文档中引用:
Counter是用于计算可哈希对象的dict子类。它是一个 无序集合,其中元素存储为字典键和 他们的计数存储为字典值。计数是允许的 任何整数值,包括零或负数。柜台班 类似于其他语言的包或多重集。
>>> c = Counter(['eggs', 'ham'])
>>> c['bacon'] # count of a missing element is zero
0
答案 1 :(得分:1)
您可以使用operator.itemgetter()
查看已排序的词典:
from operator import itemgetter
wordcount = {'test': 1, 'hello': 3, 'test2':0}
sortedWords = sorted(wordcount.items(), key=itemgetter(1), reverse = True)
输出:
>>> sortedWords
[('hello', 3), ('test', 1), ('test2', 0)]
答案 2 :(得分:0)
这应该适合你: -
ally = open("alice.txt", "r")
wordcount={}
for word in ally.read().split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
for k,v, in sorted(wordcount.items(), key=lambda words: words[1], reverse = True):
print(k,v)