【问题标题】:Calling range of dictionary in pythonpython中字典的调用范围
【发布时间】: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


【解决方案1】:

没有排序字典这样的东西。字典通常按插入顺序保存内容,但不应依赖这一点。

要保持顺序,您需要使用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)

【讨论】:

  • 从 Python 3.8 开始,您应该能够依赖它,尽管我同意它的风格通常很差。
  • @Barmar 啊,真的。我像往常一样落后于时代。仍然觉得依赖插入顺序是错误的,更清晰地明确地执行它,并且作为奖励它可以与更旧版本的 Python 一起使用。
【解决方案2】:

您可以使用 itertools.islice(sorted_dict,20) 获取前 20 个条目的迭代器。

import itertools

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 itertools.islice(sorted_dict,20):
        print(w, sorted_dict[w])
        
wordcount(book)

【讨论】:

    【解决方案3】:

    你最好使用集合模块中的计数器

    from collections import Counter 
    

    然后将 wordlist 传递给它:

    Counter(wordlist.split())
    

    【讨论】:

    • 添加如何使用这个获得前20名。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多