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

问题描述 投票:-1回答:1

我想找到出现频率为>= 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])

我知道我必须在字典中加一个条件,但我不知道是哪个条件。

python dictionary frequency
1个回答
2
投票

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

from collections import Counter

# I think your data is just a list. So image you have
data = ['the', 'box', 'and', 'the','cat', 'are', 'in', 'that', 'other', 'box']
# List the words we don't like
bad_words = ['the','to', 'a', 'and']
# Filter these words out
words = [word for word in data if word not in bad_words]
# Get the counts
counter = Counter(words)

结果(如果需要,您可以将其转换为常规字典)

Counter({'box': 2, 'cat': 1, 'are': 1, 'in': 1, 'that': 1, 'other': 1})

最后,您对单词数进行过滤(在这种情况下为空)

{word: count for word,count in counter.items() if count>=30}
© www.soinside.com 2019 - 2024. All rights reserved.