Skip to content

Latest commit

 

History

History
56 lines (30 loc) · 645 Bytes

python.dict.常用操作.md

File metadata and controls

56 lines (30 loc) · 645 Bytes

dict 常用操作

字典合并的几种常用方式

方式一

dic1 = { "a":3, "b": "3" }

dic2 = { "c":4, "d": "5", "a": 4 }

dic3 = {}

dic3.update(dic1)
print(dic3)
dic3.update(dic2)
print(dic3)
{'a': 3, 'b': '3'}
{'a': 4, 'b': '3', 'c': 4, 'd': '5'}
## 方式二
dic1 = {"a": 3, "b": "3"}

dic2 = {"c": 4, "d": "5", "a": 4}

dic3=dict(dic1,**dic2)

print(dic3)
{'a': 4, 'b': '3', 'c': 4, 'd': '5'}

方式三(推荐)

dic1 = {"a": 3, "b": "3"}

dic2 = {"c": 4, "d": "5", "a": 4}

dic3 = {**dic1, **dic2}

print(dic3)
{'a': 4, 'b': '3', 'c': 4, 'd': '5'}