只打印特定数量的 Counter 项目,格式得体

Only print specific amount of Counter items, with decent formatting

正在尝试打印出文本文件中最常用的前 N ​​个单词。到目前为止,我有文件系统和计数器,一切正常,只是想不出如何以漂亮的方式打印我想要的一定数量。这是我的代码。

import re
from collections import Counter

def wordcount(user):
"""
Docstring for word count.
"""
file=input("Enter full file name w/ extension: ")
num=int(input("Enter how many words you want displayed: "))

with open(file) as f:
  text = f.read()

words = re.findall(r'\w+', text)

cap_words = [word.upper() for word in words]

word_counts = Counter(cap_words)


char, n = word_counts.most_common(num)[0]
print ("WORD: %s \nOCCURENCE: %d " % (char, n) + '\n')

基本上,我只想进行某种循环,打印出以下内容...

例如num=3

所以它会打印出 3 个最常用的单词,以及它们的数量。 单词:Blah 出现次数:3 词语:血 出现次数:2 词语:blee 出现次数:1

保存最常见的元素并使用循环。

common = word_counts.most_common(num)[0]
for i in range(3):
    print("WORD: %s \nOCCURENCE: %d \n" % (common[i][0], common[i][1]))

我将按如下方式迭代 "most common":

most_common = word_counts.most_common(num)  # removed the [0] since we're not looking only at the first item!    
for item in most_common:
        print("WORD: {} OCCURENCE: {}".format(item[0], item[1]))

两条评论:
1. 使用 format() 来格式化字符串而不是 % - 你稍后会感谢我的建议!
2. 这样您就可以迭代 任何 个 "top N" 结果而无需将“3”硬编码到您的代码中。