This question already has answers here:
python: tuple of dictionary to Dictionary
                                
                                    (6个答案)
                                
                        
                        
                            Dict merge in a dict comprehension
                                
                                    (4个答案)
                                
                        
                                在10个月前关闭。
            
                    
在Python 3.5中,如果我有两个具有相同键和整数值的字典,则可以这样添加它们:

x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
z = {**x, **y}


但是,如果我有一长串共享xy格式的字典怎么办?在这种情况下,z的语法是什么?

最佳答案

对于列表中任意数量的字典,只需使用循环即可,这是惯用的方式:

z = {}
for d in list_of_dicts:
    z.update(d)

08-16 05:48