如何打印一个单词的排名?

时间:2013-12-14 21:53:13

标签: python

我有一个代码,用于对从网址导入的txt文件中的单词数进行排名。

from string import punctuation
from operator import itemgetter

N = 20
words = {}

words_gen = (word.strip(punctuation).lower() for line in open("output.txt")
                                     for word in line.split())

for word in words_gen:
    words[word] = words.get(word, 0) + 1

top_words = sorted(words.items(), key=itemgetter(1), reverse=True)[:N]


for word, frequency in top_words:
  print("%s %d" % (word, frequency))

因此,从文本文件中,它将返回:

hello 8
him 5
your 4

代码进行排名,但有没有办法打印出排名?

我希望它看起来像这样。

hello 1 8
him 2 5
your 3 4

第一个数字是整体排名,第二个数字是该文本文件中单词的频率。我试着计算排名。

rank=0
if top_words:
  rank+=1

但我不知道从哪里去。

2 个答案:

答案 0 :(得分:2)

您可以使用enumerate

for i, (word, frequency) in enumerate(top_words, start=1):
    print("%s %d %d" % (word, i, frequency))

答案 1 :(得分:0)

rank = 0    

for word, frequency in top_words:
  rank += 1
  print("%s %d %d" % (word,rank, frequency))

这应该有效。