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:
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:
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
.
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
question from:
https://stackoverflow.com/questions/65843858/count-frequency-of-words-inside-a-list-in-a-dictionary 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…