Reputation: 3141
I have a word occurrence dictionary, and a synonym dictionary.
Word occurrence dictionary example:
word_count = {'grizzly': 2, 'panda': 4, 'beer': 3, 'ale': 5}
Synonym dictionary example:
synonyms = {
'bear': ['grizzly', 'bear', 'panda', 'kodiak'],
'beer': ['beer', 'ale', 'lager']
}
I would like to comibine/rename aggregate the word count dictionary as
new_word_count = {'bear': 6, 'beer': 8}
I thought I would try this:
new_dict = {}
for word_key, word_value in word_count.items(): # Loop through word count dict
for syn_key, syn_value in synonyms.items(): # Loop through synonym dict
if word_key in [x for y in syn_value for x in y]: # Check if word in synonyms
if syn_key in new_dict: # If so:
new_dict[syn_key] += word_value # Increment count
else: # If not:
new_dict[syn_key] = word_value # Create key
But this isn't working, new_dict ends up empty. Also, is there an easier way to do this? Maybe using dictionary comprehension?
Upvotes: 3
Views: 156
Reputation: 46533
Using dict comprehension, sum
and dict.get
:
In [11]: {w: sum(word_count.get(x, 0) for x in ws) for w, ws in synonyms.items()}
Out[11]: {'bear': 6, 'beer': 8}
Using collections.Counter
and dict.get
:
from collections import Counter
ec = Counter()
for x, vs in synonyms.items():
for v in vs:
ec[x] += word_count.get(v, 0)
print(ec) # Counter({'bear': 6, 'beer': 8})
Upvotes: 4
Reputation: 114005
Let's change your synonym dictionary a little. Instead of mapping from a word to a list of all its synonyms, let's map from a word to its parent synonym (i.e. ale
to beer
). This should speed up lookups
synonyms = {
'bear': ['grizzly', 'bear', 'panda', 'kodiak'],
'beer': ['beer', 'ale', 'lager']
}
synonyms = {syn:word for word,syns in synonyms.items() for syn in syns}
Now, let's make your aggregate dictionary:
word_count = {'grizzly': 2, 'panda': 4, 'beer': 3, 'ale': 5}
new_word_count = {}
for word,count in word_count:
word = synonyms[word]
if word not in new_word_count:
new_word_count[word] = 0
new_word_count[word] += count
Upvotes: 1