Python 如何查找具有一定出现频率的所有单词,不包括某些单词

Python 如何查找具有一定出现频率的所有单词,不包括某些单词,python,dictionary,frequency,Python,Dictionary,Frequency,我想查找出现频率>=30的所有单词,不包括单词the、and、to和a 我尝试了以下代码: import json from pprint import pprint with open ('clienti_daune100.json') as f: data=json.load(f) word_list=[] for rec in data: word_list=word_list + rec['Dauna'].lower().split() print(wor

我想查找出现频率>=30的所有单词,不包括单词the、and、to和a

我尝试了以下代码:

import json
from pprint import pprint

with open ('clienti_daune100.json') as f:
    data=json.load(f)

word_list=[]

for rec in data:   
      word_list=word_list + rec['Dauna'].lower().split()
print(word_list[:100], '...', len(word_list), 'Total words.' )

dict = {}

for word in word_list:
    if word not in dict:
        dict[word] = 1
    else:
        dict[word] += 1

w_freq = []

for key, value in dict.items():
    w_freq.append((value, key))   

w_freq.sort(reverse=True)
pprint(w_freq[:100])

我知道我必须在字典中添加一个条件,但我无法确定是哪一个条件。

首先过滤数据,然后您可以使用itertools.Counter

如果需要,您可以将其转换为常规dict

Counter({'box': 2, 'cat': 1, 'are': 1, 'in': 1, 'that': 1, 'other': 1})
最后,对本例中为空的字数进行筛选

{word: count for word,count in counter.items() if count>=30}

定义一组排除的单词,然后输入if value>=30,并输入not in excluded_单词:在最后一个循环中,您只需添加想要添加的单词w_freq.Gecko已经给出了一个很好的答案,但我要补充的是,您应该小心调用变量dict,因为它是一个内置的方法,会被掩盖。
{word: count for word,count in counter.items() if count>=30}