Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将两本词典合并为一本_Python - Fatal编程技术网

Python 将两本词典合并为一本

Python 将两本词典合并为一本,python,Python,我尝试执行以下Python代码,但解释器过早返回。我相信这是因为我试图将两本词典合并在一起,就像它们是列表一样: both = {} one = {"A" : 0} two = {"B" : 0} both = one + two // Returns prematurely here 我怎样才能把两本词典合并成一本呢?你试过一本了吗?它将为您提供所需的组合dict。但您需要小心,因为这两个dict中的键都被覆盖 both = {} one = {"A" : 0} two = {"B"

我尝试执行以下Python代码,但解释器过早返回。我相信这是因为我试图将两本词典合并在一起,就像它们是列表一样:

both = {}
one = {"A" : 0}
two = {"B" : 0}
both = one + two    // Returns prematurely here
我怎样才能把两本词典合并成一本呢?

你试过一本了吗?它将为您提供所需的组合dict。但您需要小心,因为这两个dict中的键都被覆盖

both = {}
one = {"A" : 0}
two = {"B" : 0}
both.update(one)
both.update(two)
print one
print both