方法一:使用+连接
d1={'user':'root','passwd':'123456'}
d2={'ip':'192.168.0.10','port':3306}
#d3=dict(d1.items()+d2.items())
#print(d3)

该方法只在python2中可以使用,python3中已经不支持会报TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'

方法二:使用update方法
d4={}
d4.update(d1)
d4.update(d2)
print(d4)
方法三:借助字典的dict(d1, **d2)方法
d5=dict(d1,**d2)
print(d5)
方法四:对字典遍历输出
d6={}
for k,v in d1.items():
    d6[k]=v
for k,v in d2.items():
    d6[k]=v
print(d6)

 

posted on 2018-08-20 18:40  小狐狸记录测试点点滴滴  阅读(234)  评论(0编辑  收藏  举报