我有两本词典:
fruit1 = {'apple': 3, 'banana': 1, 'cherry': 1}
fruit2 = {'apple': 42, 'peach': 1}
Run Code Online (Sandbox Code Playgroud)
我想要的最终结果是:
inv3 = {'apple': 45, 'banana': 1, 'cherry': 1, 'peach': 1}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试过这个示例代码,因为这个输出看起来几乎与我想要的类似,只是它没有按照我想要的方式打印,而是接近:
d1 = {'apple': 3, 'orange': 1,}
d2 = {'apple': 42, 'orange': 1}
ds = [d1, d2]
d = {}
for k in d1.keys():
d[k] = tuple(d[k] for d in ds)
print(ds)
Run Code Online (Sandbox Code Playgroud)
输出将是这样的:
[{'apple': 3, 'orange': 1}, {'apple': 42, 'orange': 1}]
Run Code Online (Sandbox Code Playgroud)
当我尝试使用示例代码输入我的两个词典时:
fruit1 = {'apple': 3, 'banana': 1, 'cherry': 1}
fruit2 = {'apple': 42, …Run Code Online (Sandbox Code Playgroud)