我正在尝试将字典组合成一个字典,同时保留以前的值。我在下面提供了一个示例代码。理想情况下,我正在尝试完成以下任务:dict3 = {"dict1" : {"North America" : "United States" , "Europe" : "France" , "Asia" : "China" }, "dict2" : {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}}dict1 = {"North America" : "United States" , "Europe" : "France" , "Asia" : "China"}dict2 = {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}dict3 = {}targetdict = {"dict1" : {"North America" : "United States" , "Europe" : "France" , "Asia" : "China" }, "dict2" : {"North America" : "Canada" , "Europe" : "Spain" , "Asia" : "Japan"}}dict3.update(dict1)print(dict3)dict3.update(dict2)print(dict3)我希望 dict3 与 targetdict 相同。所以有效地 print(dict3) 与 print(targetdict) 的结果相同。我希望将许多潜在的字典添加到“dict3”变量中,例如,所有字典都可以将“北美”作为键。任何反馈将不胜感激!
添加回答
举报
0/150
提交
取消