聚合和重命名字典中的键

Aggregating and Renaming Keys in Dictionary

我有一个单词出现词典和一个同义词词典。

单词出现字典示例:

word_count = {'grizzly': 2, 'panda': 4, 'beer': 3, 'ale': 5}

同义词词典示例:

synonyms = {
            'bear': ['grizzly', 'bear', 'panda', 'kodiak'],
            'beer': ['beer', 'ale', 'lager']
           }

我想comibine/rename将单词统计词典聚合为

new_word_count = {'bear': 6, 'beer': 8}

我想我会试试这个:

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

但这不起作用,new_dict 最终为空。另外,有没有更简单的方法来做到这一点?也许使用字典理解?

使用字典理解,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}

使用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})

让我们稍微修改一下您的同义词词典。我们不是从一个词映射到它所有同义词的列表,而是从一个词映射到它的父同义词(即 alebeer)。这应该加快查找速度

synonyms = {
            'bear': ['grizzly', 'bear', 'panda', 'kodiak'],
            'beer': ['beer', 'ale', 'lager']
           }
synonyms = {syn:word for word,syns in synonyms.items() for syn in syns}

现在,让我们制作聚合字典:

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