python - python中字典的调用范围
问题描述
我有一个 .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 个呢?
解决方案
没有排序字典之类的东西。字典通常按插入顺序保存内容,但不应依赖此。
为了保持顺序,您需要使用OrderedDict
.
from collections import OrderedDict
newDict = OrderedDict()
for k,v in sorted(freq_dict.items(),key = lambda kv: kv[1], reverse = True)):
newDict[k] = v
然后您可以执行以下操作:
for pos,(k,v) in enumerate(newDict.items()):
if pos < 20:
print(pos,k,v)
推荐阅读
- android - Android TextView TextWatcher
- javascript - 为什么要通过方括号访问 Symbol.iterator?
- angular - 函数返回后变量未定义,需要点击两次才能设置变量
- android - “等待调试器”而不处于调试模式
- javascript - 如何使用 .innerHTML 添加 html 标签?
- python - Python ValueError:解包的值太多(带有嵌套变量的 For 循环)
- c - 未知类型的可变参数
- swift - 如何从文本字段中关闭键盘
- wordpress - Wordpress + nginx + ALB + SSL -> 链接问题
- python - ModuleNotFoundError:没有名为“tensorflow.contrib.framework”的模块