将三个Python字典组合在一起

将三个Python字典组合在一起,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,我正在尝试将三个Python字典组合在一起 它有点像下面: dict1 = {'key1' : ['value1','value2','value3'] , 'key2' : ['value1','value5','value6']} dict2 = {'value1' : ['test1','test1','test2','test3','test4'], 'value2': ['test1','test2','test2','test5','test6']........continues}

我正在尝试将三个Python字典组合在一起

它有点像下面:

dict1 = {'key1' : ['value1','value2','value3'] , 'key2' : ['value1','value5','value6']}
dict2 = {'value1' : ['test1','test1','test2','test3','test4'], 'value2': ['test1','test2','test2','test5','test6']........continues}
dict3 = {'test1' : [0,1,4,5], 'test2' : '[3,1,2,0]', test3:[7,8,1,3,4].... continues}
mainDict = {'key1':[  {'value1' : { 'test1' :[0,1,4,5],'test2' : [3,1,2,0],'test3' :[7,8,1,3,4]...continues} } ]}
我想创建一个字典,如下所示:

dict1 = {'key1' : ['value1','value2','value3'] , 'key2' : ['value1','value5','value6']}
dict2 = {'value1' : ['test1','test1','test2','test3','test4'], 'value2': ['test1','test2','test2','test5','test6']........continues}
dict3 = {'test1' : [0,1,4,5], 'test2' : '[3,1,2,0]', test3:[7,8,1,3,4].... continues}
mainDict = {'key1':[  {'value1' : { 'test1' :[0,1,4,5],'test2' : [3,1,2,0],'test3' :[7,8,1,3,4]...continues} } ]}
我怎样才能把这三本词典合并成一本呢


[我使用的是python 2.7版本]

这可以通过简单地循环字典并为每个值分配子字典来实现

finaldict = {}
for key in dict1.keys():
    finaldict[key] = {}
    for key2 in dict1[key]:
        finaldict[key][key2] = {}
        for key3 in dict2[key2]:
            finaldict[key][key2][key3] = dict3[key3]
print finaldict
我不得不删除很多值和测试,因为其他字典中并没有引用这些值和测试。既然您使用了“…continue”,我假设您在某个地方有它们,尽管提供在问题中实际工作的结构是很好的。如果希望缺少的键引用默认为某个值,例如None,请查看collections模块中的defaultdict。

您预期的输出实际上不是有效的Python代码。你自己试过什么了吗?你在哪里卡住了?你好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能也想了解一下。@MorganThrapp:谢谢。我将通过链接。很高兴能帮助!如果此答案对您有效,请将其作为正确答案接受,以便其他用户在遇到类似问题时可以看到此答案有效!