【发布时间】:2021-11-04 16:14:33
【问题描述】:
我有一个 .txt 文件中单词的排序字典(降序)及其频率。例如,{'the':1682}。我需要编写代码,以便只打印最常见的 20 个单词(因为它们已经被订购,所以只打印前 20 个项目)。我知道字典是按插入排序的,但是我不知道如何利用这个告诉 python 打印出前 20 个。这是我的代码
def wordcount(book):
single_list = []
unique = []
freq_dict = {}
for word in wordlist:
no_punc = word.strip(punctuation)
lower_case = no_punc.lower()
single_list.append(lower_case)
unique = set(single_list)
#num_unique = print(len(unique))
for word in single_list:
if word in freq_dict:
freq_dict[word] += 1
else:
freq_dict[word] = 1
sorted_dict = dict(sorted(freq_dict.items(), key = lambda kv: kv[1], reverse = True))
for w in sorted_dict:
print(w, sorted_dict[w])
wordcount(book)
输出是
the 1632
and 845
to 721
a 627
she 537
it 526
of 508
said 462
i 401
alice 386
in 367
you 362
was 357
that 276
as 262
her 248
at 210
on 193
with 180
all 180
had 178
but 166
for 153
so 150
be 146
very 144
not 144
what 136
this 134
little 128
they 127
he 120
out 113
is 102
down 101
one 101
up 98
his 96
about 94
if 94
then 90
no 87
know 86
like 85
were 85
them 84
would 83
went 83
herself 83
again 82
do 81
have 80
when 79
could 77
or 76
there 75
thought 74
off 73
time 68
me 68
queen 68
对书中的每个单词(约 2800 个单词)依此类推。那么如何让 python 只打印前 20 个呢?
【问题讨论】:
-
你可以在循环中使用
enumerate,当索引达到20时使用break。 -
你可以使用
for w in list(sorted_dict.keys())[:20]: print(w, sorted_dict[w]) -
您不使用
collections.Counter()有什么原因吗?它有一个most_common()方法,可以让你请求前20名。 -
@SuneeshJacob 你不需要
.keys()。使用字典作为序列返回键。 -
@Barmar 哦,我明白了。谢谢提供信息。顺便说一句,似乎
itertools.islice(sorted_dict,20)在这种情况下效果更好。
标签: python dictionary indexing