首先生成两个字典,如下:
dict_one = {'one': '1', 'two': '2'}
dict_two = {'three': '3', 'tour': '4'}
方法一: 使用uptate方法,把two字典更新至one字典中
dict_one.update(dict_two)
print(dict_one)
方法二: 首先使用copy方法,把one字典进行了复制,然后使用update方法把two字典更新到three字典中
dict_three = dict_one.copy()
dict_three.update(dict_two)
print(dict_three)
方法三:使用for循环, 利用迭代器(以(键,值)为元素的元组表示)
for k, y in dict_one.items():
dict_two[k] = y
print(dict_two)
方法四:对字典two进行拆包,再把one和two的组合转成字典类型
dict_four = dict(dict_one, **dict_two)
print(dict_four)
如有疑义,欢迎找茬…