合并嵌套的默认值

时间:2012-05-30 17:05:40

标签: python data-structures union defaultdict

我有这个:

dict1 = defaultdict(lambda:defaultdict(list))
dict1['rl1']['sh1'] = ['a','b']
dict1['rl1']['sh2'] = ['c','d']
dict1['rl2']['sh1'] = ['c','d']

dict2 = defaultdict(lambda:defaultdict(list))
dict2['rl1']['sh1'] = ['p','q']
dict2['rl1']['sh3'] = ['r','s']
dict2['rl3']['sh1'] = ['r','s']

我想做两个默认配置的联合,这将是结果:

uniondict = defaultdict(lambda:defaultdict(list))
uniondict['rl1']['sh1'] = ['a','b','p','q']
uniondict['rl1']['sh2'] = ['c','d']
uniondict['rl1']['sh3'] = ['r','s']
uniondict['rl2']['sh1'] = ['c','d']
uniondict['rl3']['sh1'] = ['r','s']

我不确定如何获得这个结果..我尝试过使用dict1.items()和dict2.items(),或更新功能,但我必须遗漏一些因为我无法得到违约的“联合”。

2 个答案:

答案 0 :(得分:7)

略微'优雅':

uniondict = defaultdict(lambda:defaultdict(list))
for k1, v1 in dict1.items() + dict2.items():
    for k2, v2 in v1.items():
        uniondict[k1][k2] += v2

以获得更具内存效率的解决方案:

from itertools import chain
uniondict = defaultdict(lambda:defaultdict(list))
for k1, v1 in chain(dict1.iteritems(), dict2.iteritems()):
    for k2, v2 in v1.iteritems():
        uniondict[k1][k2] += v2

这将使用迭代器来阻止在内存中创建临时列表

itertools.chain docs


对于后代,这里是一个泛型函数,它将合并嵌套的字典对象(defaultdict或no),其中第二层值支持+运算符。 (列表,整数,集等):

from collections import defaultdict
from itertools import chain

def merge_nested_dicts(dict_list):
    uniondict = defaultdict(lambda:defaultdict(list))
    for k1, v1 in chain(*[d.iteritems() for d in dict_list]):
        for k2, v2 in v1.iteritems():
            uniondict[k1][k2] += v2
    return uniondict

答案 1 :(得分:4)

dict12 = defaultdict(lambda:defaultdict(list))
for k,v in dict1.items():
    for k1,v1 in v.items():
        dict12[k][k1] = v1
for k,v in dict2.items():
    for k2,v2 in v.items():
        dict12[k][k2] += v2
for k,v in dict12.items():
    for k12,v12 in v.items():
        print "dict12[%r][%r] = %r" % (k,k12,v12)