结合词典Python中多个键的值

时间:2016-10-11 19:45:02

标签: python dictionary

在Python中,我有以下字典集:

{
    1: {'Hello', 'Bye'},
    2: {'Bye', 'Do', 'Action'},
    3: {'Not', 'But', 'No'},
    4: {'No', 'Yes'}
}

我的目标是组合包含匹配值的键(例如,在此示例中," Bye"和" No"),因此结果将如下所示:

{
    1: {'Hello', 'Bye', 'Do', 'Action'},
    3: {'Not', 'But', 'No', 'Yes'}
}

有办法做到这一点吗?

2 个答案:

答案 0 :(得分:1)

如果没有重叠匹配:

a = {1: {'Hello', 'Bye'}, 2: {'Bye', 'Do', 'Action'}, 3: {'Not', 'But', 'No'}, 4: {'No', 'Yes'}}
output = {}
for k, v in a.items():
    if output:
        for k_o, v_o in output.items():
            if v_o.intersection(v):
                output[k_o].update(v)
                break
        else:
            output[k] = v
    else:
        output[k] = v
print(output)

输出:

{1: {'Action', 'Bye', 'Do', 'Hello'}, 3: {'But', 'No', 'Not', 'Yes'}}

答案 1 :(得分:1)

如果匹配重叠且你想要最长的匹配:

from collections import defaultdict

d = {
    1: {'Hello', 'Bye'},
    2: {'Bye', 'Do', 'Action'},
    3: {'Not', 'But', 'No'},
    4: {'No', 'Yes'}
}
grp = defaultdict(list)

# first group all keys with common words
for k, v in d.items():
    for val in v:
        grp[val].append(k)


# sort the values by lengths to find longest matches.    
for v in sorted(grp.values(), key=len, reverse=True):
    for val in v[1:]:
       if val not in d:
           continue
           # use first ele as the key and union to existing values
       d[v[0]] |= d[val]
       del d[val]


print(d)

如果你没有重叠,你可以:

grp = defaultdict(list)

for k, v in d.items():
    for val in v:
        grp[val].append(k)

for v in grp.values():
    for val in v[1:]:
        d[v[0]] |= d[val]
        del d[val]

或者如果你想要一个新的词典:

new_d = {}
for v in grp.values():
    if len(v) > 1:
        k = v[0]
        new_d[k] = d[k]
        for val in v[1:]:
            new_d[k] |= d[val]

所有三个都给你以下但关键顺序可能不同:

{1: set(['Action', 'Do', 'Bye', 'Hello']), 3: set(['Not', 'Yes', 'But', 'No'])}