2016-06-10 38 views
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 wordcount.items(): 
    print(k,v) 

回答

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

只需使用Counter。它将缩短您的代码并获得您想要的订购。

从文档引用:

A计数器是用于计数可哈希对象的字典子类。它是一个无序集合,其中元素作为字典键存储,并且它们的计数作为字典值存储在 中。计数允许为任何整数值,包括零或负计数。柜台类 类似于其他语言的箱包或多配套。

>>> c = Counter(['eggs', 'ham']) 
>>> c['bacon']        # count of a missing element is zero 
0 
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)