Python中实现,将两个字典变量合并,并且覆盖已有的值,参考代码如下:
print 'dict test ++++++++++++++++++' dict1 = {1:111, 2:333} dict2 = {2:22, 4:4444} dict3 = {} dict3 = dict1 #for key in dict1: # dict3[key] = dict1[key] for key in dict2.keys(): print key dict3[key] = dict2[key] #dict3 = dict1.update(dict2) print "dict3=",dict3 # dict test ++++++++++++++++++ # 2 # 4 # dict3= {1: 111, 2: 22, 4: 4444}
转载请注明:在路上 » 【整理】Python中合并两个字典变量(运行覆盖已有的值)