用类似的键合并python中的Dict

2024-05-23 19:46:05 发布

您现在位置:Python中文网/ 问答频道 /正文

data1 = {'Height': {'Ajay': 145.830651784016,
  'Bist': 145.91779226416696,
  'Kay': 146.16402145150246}
}
 
data2 = {'Height': {'Kim': 155.830651784016,
  'DDD': 155.91779226416696,
  'Arambh': 156.16402145150246}
}

data3 = {'Height': {'Dong': 165.830651784016,
  'Ding': 165.91779226416696,
  'Dumba': 166.16402145150246}
}

以上是我想合并成一本的三本字典。 预期产出如下:

den_w = {'Height': {'Ajay': 145.830651784016,
      'Bist': 145.91779226416696,
      'Kay': 146.16402145150246,
      'Kim': 155.830651784016,
      'DDD': 155.91779226416696,
      'Arambh': 156.16402145150246,
      'Dong': 165.830651784016,
      'Ding': 165.91779226416696,
      'Dumba': 166.16402145150246}
        }

下面是我的尝试。没有达到预期产出:

尝试1失败:

den_w = {**data1, **data2, **data3}

尝试2失败:

den_w = {k: [data1.get(k, {}), data2.get(k, {}), data3.get(k, {})] for k in data1.keys() | data2.keys() | data3.keys()}

尝试3失败:

dicts = data1,data2,data3

尝试4失败:

dicts = {}
dicts.update(data1)
dicts.update(data2)
dicts.update(data3)

有没有办法达到预期的产量


Tags: getupdatekeysheightdictsdddkimdata1
3条回答

试试这个:

def merge(a, b):
    for key in b:
        if key in a:
            if isinstance(a[key], dict) and isinstance(b[key], dict):
                merge(a[key], b[key])
            elif a[key] == b[key]:
                pass             
        else:
            a[key] = b[key]
    return a

den_w  = merge(data1, data2)
den_w = merge(den_w, data3)

输出:

{'Height': {'Ajay': 145.830651784016,
  'Arambh': 156.16402145150246,
  'Bist': 145.91779226416696,
  'DDD': 155.91779226416696,
  'Ding': 165.91779226416696,
  'Dong': 165.830651784016,
  'Dumba': 166.16402145150246,
  'Kay': 146.16402145150246,
  'Kim': 155.830651784016}}

找到所有dict之间的公共键,然后合并它们

{k: {**data1[k], **data2[k], **data3[k]} 
 for k in set(data1.keys()).intersection(
     set(data2.keys())).intersection(set(data3.keys()))}

输出:

{'Height': {'Ajay': 145.830651784016,
  'Arambh': 156.16402145150246,
  'Bist': 145.91779226416696,
  'DDD': 155.91779226416696,
  'Ding': 165.91779226416696,
  'Dong': 165.830651784016,
  'Dumba': 166.16402145150246,
  'Kay': 146.16402145150246,
  'Kim': 155.830651784016}}

编辑:仅使用链图的方法

这里有一个单行线来做这个。它对字典中每个唯一键的子指令进行分组和组合

from collections import ChainMap

L = [data1, data2, data3]

{k:dict(ChainMap(*[i.get(k) for i in L])) for k in dict(ChainMap(*L)).keys()}
{'Height': {'Dong': 165.830651784016,
  'Ding': 165.91779226416696,
  'Dumba': 166.16402145150246,
  'Kim': 155.830651784016,
  'DDD': 155.91779226416696,
  'Arambh': 156.16402145150246,
  'Ajay': 145.830651784016,
  'Bist': 145.91779226416696,
  'Kay': 146.16402145150246}}

在演示案例上进行测试-

d1 = {'a':{'x':1, 'y':2}, 'b':{'l':1, 'm':2}}
d2 = {'a':{'i':1, 'j':2}, 'b':{'w':1, 'z':2}}

L = [d1, d2]

{k:dict(ChainMap(*[i.get(k) for i in L])) for k in dict(ChainMap(*L)).keys()}
#dictionaries for key a and b are respectively combined
{'a': {'i': 1, 'j': 2, 'x': 1, 'y': 2}, 
 'b': {'w': 1, 'z': 2, 'l': 1, 'm': 2}}

下面是一种使用collections.defaultdictcollections.ChainMap的pythonic方法。我已经解释了下面的步骤

from collections import ChainMap, defaultdict

L = [data1, data2, data3]
d = defaultdict(dict)

for key in dict(ChainMap(*L)).keys():
    for i in L:
        d[key].update(i.get(key))

d = dict(d)
print(d)
{'Height': {'Dong': 165.830651784016,
  'Ding': 165.91779226416696,
  'Dumba': 166.16402145150246,
  'Kim': 155.830651784016,
  'DDD': 155.91779226416696,
  'Arambh': 156.16402145150246,
  'Ajay': 145.830651784016,
  'Bist': 145.91779226416696,
  'Kay': 146.16402145150246}}
  1. defaultdict(dict)存储一个或多个dict
  2. dict(ChainMap(*L)).keys()获取列表中所有字典中存在的唯一键。在这种情况下,它只是height
  3. 对于列表中的每个key和每个元素,键的字典将用该key的值更新
  4. 将此d转换为dict

相关问题 更多 >