通过键和值组合多个字典?

Combine multiple dictionaries by keys and values?

经过几个小时和其他帖子的建议,我无法解决我的问题。 我必须管理很多词典(直到现在,这是我知道的唯一工作方式)。

对于我要合并的所有四个词典,其中三个具有相同的键(d1、d2 和 d3)。

d1 = {key1: [x1, x2, x3], key2: [y1, y2, y3], key3: [z1, z2, z3]}
d2 = {key1: [x4, x5, x6],key2: [y4, y5, y6], key3: [z4, z5, z6]}
d3 = {key1: [x7, x8, x9], key2: [y7, y8, y9], key3: [z7, z8, z9]}
d4 = {x2: [a, b, c], y2: [d, e, f], z2: [g, h, i]}

第四个字典是由包含元数据的参考文件生成的字典,它们的键等于 d1 中的一个值,我想要的是用 d1, d2 中的信息创建一个字典and d3 and after include the information of d4 in the final dictionary.

final_dict = {key1: [x1, a, b, x2, x3, x4, x5, x8, x9],
              key2: [y1, d, e, y2, y3, y4, y5, y8, y9],
              key3: [z1, g, h, z2, z3, z4, z5, z8, z9]}

并以表格格式打印如下:

key1  x1  a  b  x2  x3  x4  x5  x8  x9
key2  y1  d  e  y2  y3  y4  y5  y8  y9
key3  z1  g  h  z2  z3  z4  z5  z8  z9

目前我有一个脏脚本但是 "works"。

#!/usr/bin/env python

with open("file1.txt", "r") as file1, open("file2.txt", "r") as file2, /
     open("file3.txt", "r") as file3, open("file4.txt", "r") as file4:

    d1 = {}
    d2 = {}
    d3 = {}
    d4 = {}
    dicts = [d1, d2, d3, d4]

    #d1 = {key1: [x1, x2, x3], key2: [y1, y2, y3], key3: [z1, z2, z3]}
    #d2 = {key1: [x4, x5, x6],key2: [y4, y5, y6], key3: [z4, z5, z6]}
    #d3 = {key1: [x7, x8, x9], key2: [y7, y8, y9], key3: [z7, z8, z9]}
    #d4 = {x2: [a, b, c], y2: [d, e, f], z2: [g, h, i]}

    for b in file1:
        row = b.strip().split('\t')
        if row[0] not in d1:
            d1[row[0]] = row[1], row[3], row[4]

    for c in file2:
        row = c.strip().split('\t')
        if row[0] not in d2:
            d2[row[0]] = row[1:]

    for f in file3:
        row = f.strip().split('\t')
        if row[0] not in d3:
            d3[row[0]] = row[1:]

    for m in file4:
        row = m.strip().split('\t')
        if row[0] not in d4:
            d4[row[0]] = row[1], row[3], row[2]

    final_dict = {}
    for k in (dicts):
        for key, value in k.iteritems():
            final_dict[key].append(value)

    print final_dic

    #key1  x1  a  b  x2  x3  x4  x5  x8  x9
    #key2  y1  d  e  y2  y3  y4  y5  y8  y9
    #key3  z1  g  h  z2  z3  z4  z5  z8  z9

问题是最后 3 行。

由于缺乏深入的知识,将不胜感激简单的建议(对于傻瓜)。

我想这就是你要找的东西,尽管关于为什么像 x6x7y6y7 等变量的逻辑是错误的。被排除在外尚不清楚:

首先,让这些变量(例如x1x2等)存在,并为它们的值指定它们自己的名称作为字符串,以便以后更容易跟踪结果:

values = [letter + str(number) for letter in 'xyz' for number in range(1, 10)] + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
for v in values:
    exec('%s = "%s"' % (v, v))

接下来,让我们实例化您的字典:

d1 = {key1: [x1, x2, x3], key2: [y1, y2, y3], key3: [z1, z2, z3]}  
d2 = {key1: [x4, x5, x6],key2: [y4, y5, y6], key3: [z4, z5, z6]}
d3 = {key1: [x7, x8, x9], key2: [y7, y8, y9], key3: [z7, z8, z9]}
d4 = {x2: [a, b, c], y2: [d, e, f], z2: [g, h, i]}

然后,让我们把字典合并成一个大的,最终的 dict:

new_dict = {}
for d in [d1, d2, d3]:
    for key in d:
        if key not in new_dict:
            # if key not yet in the dict, make it so
            new_dict[key] = d[key]
        else:
            # if key already there, then we'll just add the lists together
            new_dict[key] += d[key]

最后,要从 d4 中获取前两个单独的字母,我们可以试试这个:

for key in new_dict:
    for other_key in d4:
        if other_key in new_dict[key]:
            new_dict[key] += d4[other_key][:2]

检查输出:

>>> new_dict
{'key2': ['y1', 'y2', 'y3', 'y4', 'y5', 'y6', 'y7', 'y8', 'y9', 'd', 'e'], 'key3': ['z1', 'z2', 'z3', 'z4', 'z5', 'z6', 'z7', 'z8', 'z9', 'g', 'h'], 'key1': ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'x8', 'x9', 'a', 'b']}

这基本上是您想要的结果,除了它包括 6 和 7。您能否提供一些背景知识,说明为什么您想要的输出看起来是这样的?无论如何,这应该可以帮助您入门。