我有一個 .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 個呢?
uj5u.com熱心網友回復:
沒有排序字典這樣的東西。字典通常按插入順序保存內容,但不應依賴于此。
要保持順序,您需要使用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)
uj5u.com熱心網友回復:
您可以使用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)
uj5u.com熱心網友回復:
您最好使用集合模塊中的 Counter
from collections import Counter
然后將 wordlist 傳遞給它:
Counter(wordlist.split())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/350558.html
上一篇:如何根據內部字典中的值過濾字典
