简体   繁体   English

如何串联或合并两个具有重叠键的defaultdict的defaultdict?

[英]How to concatenate or combine two defaultdicts of defaultdicts that have overlapping keys?

Working with the suggestion provided here , and having two revenue tables to have to extract from (containing the same fruits but different brands): 使用此处提供的建议,并必须提取两个收入表(包含相同的水果,但品牌不同):

在此处输入图片说明

在此处输入图片说明

I end of with two defaultdicts of defaultdicts : 我最后的两个defaultdictsdefaultdicts

d1 D1

defaultdict(<function <lambda> at 0x105dd42a8>, {u'Kiwi': defaultdict(<type 'int'>, {u'NZKiwi': 1.2}), u'Pear': defaultdict(<type 'int'>, {u'PearShaped': 6.2}), u'Banana': defaultdict(<type 'int'>, {u'BananaBrand': 4.0, u'OtherBrand': 3.2}), u'Apple': defaultdict(<type 'int'>, {u'CrunchApple': 1.7})})

d2 D2

defaultdict(<function <lambda> at 0x105dd41b8>, {u'Kiwi': defaultdict(<type 'int'>, {u'n': 1.2}), u'Pear': defaultdict(<type 'int'>, {u'p': 6.2}), u'Banana': defaultdict(<type 'int'>, {u'b': 4.0, u'o': 3.2}), u'Apple': defaultdict(<type 'int'>, {u'a': 1.7})})

Assuming for argument's sake that I cannot combine the two tables before the above processing, how do I now combine these defaultdicts such that the keys (the fruit types) are maintained, yet their respective values get concatenated, per key? 假设出于论证的原因,在上述处理之前无法合并两个表,现在如何合并这些默认值,以使键(水果类型)得以保留,而每个键的各自值却被串联了? In other words, how do I end up with the following: 换句话说,我如何得出以下结论:

defaultdict(<function <lambda> at 0x105dd41b8>, {u'Kiwi': defaultdict(<type 'int'>, {u'n': 1.2, u'NZKiwi': 1.2}), u'Pear': defaultdict(<type 'int'>, {u'p': 6.2, u'PearShaped': 6.2}), u'Banana': defaultdict(<type 'int'>, {u'b': 4.0, u'o': 3.2, u'BananaBrand': 4.0, u'OtherBrand': 3.2}}), u'Apple': defaultdict(<type 'int'>, {u'a': 1.7, u'CrunchApple': 1.7})})

Actually it doesn't necessarily have to take that structure - really what I need is something from which I can extract the individual brand-amount pairs, per product . 实际上,不一定必须采用这种结构-实际上,我需要的是可以从中提取每种产品的单个品牌数量对的东西。 So end is results is a dict for Banana containing its 4 brand-amount pairs, another dict for Kiwi containing its two brand-amount pairs etc. 因此最终结果是香蕉包含其4个品牌量对的字典,猕猴桃包含其2个品牌量对的字典等等。

UPDATE: I could do the following, but there is surely a cleaner way?? 更新:我可以执行以下操作,但是肯定有一种更清洁的方法?

BananaDict = {}
KiwiDict ={}
AppleDict = {}
PearDict = {}

for k, v in d1.iteritems():
    if k == 'Banana':
        BananaDict.update(v)
    elif k == 'Kiwi'
    etc.
    etc.

then repeat the above for d2. 然后对d2重复以上操作。

You can just use a simple for loop , example - 您可以只使用简单的for循环,例如-

>>> d = defaultdict(int, {u'Kiwi': defaultdict(int, {u'NZKiwi': 1.2}), u'Pear': defaultdict(int, {u'PearShaped': 6.2}), u'Banana': defaultdict(int, {u'BananaBrand': 4.0, u'OtherBrand': 3.2}), u'Apple': defaultdict(int, {u'CrunchApple': 1.7})})
>>> d1 = defaultdict(int, {u'Kiwi': defaultdict(int, {u'n': 1.2}), u'Pear': defaultdict(int, {u'p': 6.2}), u'Banana': defaultdict(int, {u'b': 4.0, u'o': 3.2}), u'Apple': defaultdict(int, {u'a': 1.7})})
>>> for k,v in d.items():
...     v.update(d1[k])
...
>>> d
defaultdict(<class 'int'>, {'Banana': defaultdict(<class 'int'>, {'o': 3.2, 'OtherBrand': 3.2, 'BananaBrand': 4.0, 'b': 4.0}), 'Pear': defaultdict(<class 'int'>, {'PearShaped': 6.2, 'p': 6.2}), 'Kiwi': defaultdict(<class 'int'>, {'NZKiwi': 1.2, 'n': 1.2}), 'Apple': defaultdict(<class 'int'>, {'a': 1.7, 'CrunchApple': 1.7})})

I noticed that the other answer didnt account for the case where the new dictionary had a key that the original dictionary didnt have. 我注意到另一个答案没有说明新词典具有原始词典没有的密钥的情况。

def merge_defaultdicts(d,d1):
    for k,v in d1.items():
        if (k in d):
            d[k].update(d1[k])
        else:
            d[k] = d1[k]
    return d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM