[英]Count frequency of words inside a list in a dictionary
I have a list of common keywords:我有一个常用关键字列表:
common_keywords = ['dog', 'person', 'cat']
And a list of dictionaries, containing keywords and sometimes the common_keywords
listed above:还有一个字典列表,包含关键字,有时还有上面列出的common_keywords
:
people = [{'name':'Bob', 'keywords': ['dog', 'dog', 'car', 'trampoline']},
{'name':'Kate', 'keywords': ['cat', 'jog', 'tree', 'flower']},
{'name':'Sasha', 'keywords': ['cooking', 'stove', 'person', 'cat']}]
I would like to count the frequency of the common_keywords
for each person, so the desired output would look something like:我想计算每个人的common_keywords
的频率,所以所需的 output 看起来像:
counts = [{'name': 'Bob', 'counts': [{'dog': 2}]},
{'name': 'Kate', 'counts': [{'cat': 1}]},
{'name': 'Sasha', 'counts': [{'person':1}, {'cat': 1}]]
I am able to use dict(Counter())
to count the keywords and filter them if they appear in the common_keywords
but I am struggling with linking these counts back to the original name as shown in the desired output: counts
.我可以使用dict(Counter())
来计算关键字并在它们出现在common_keywords
中时对其进行过滤,但我正在努力将这些计数链接回原始名称,如所需的 output: counts
所示。
Current code (I think I am slowly getting there):当前代码(我想我正在慢慢到达那里):
freq_dict = {}
for p in people:
name = p['name']
for c in p['keywords']:
if c not in freq_dict:
freq_dict[name] = {c: 1}
else:
if c not in freq_dict[name]:
freq_dict[c] = 1
else:
freq_dict[c] +=1
You can use a list-comprehension along with collections.Counter
which does exactly what you want with the nested list.您可以将列表理解与collections.Counter
一起使用,这正是您想要的嵌套列表。 - -
from collections import Counter
[{'name':i.get('name'),
'keywords':[dict(Counter([j for j in i.get('keywords')
if j in common_keywords]))]} for i in people]
[{'name': 'Bob', 'keywords': [{'dog': 2}]},
{'name': 'Kate', 'keywords': [{'cat': 1}]},
{'name': 'Sasha', 'keywords': [{'person': 1, 'cat': 1}]}]
i.get('key')
.首先,通过列表理解,您希望使用与i.get('key')
一起定义的键来重建原始字典列表。 This will let to work with the nested list value for keywords.这将允许使用关键字的嵌套列表值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.