1. 程式人生 > >python3兩個字典的合並

python3兩個字典的合並

ons 直接 mil right ng- usr ffffff 兩個 span

兩個字典的合並其實很簡單,直接用dict的update即可,代碼如下:

# /usr/bin/python3 
# -*- encoding: utf-8 -*-
import json
param1 = """
{"a":"1",
"b":"2"}
"""
param2 = """
{"b":"3",
"c":"4"}
"""
json1 = json.loads(param1)
json2 = json.loads(param2)
print("json1: ", json1)
print("json2: ", json2)
merge = dict(json1)
merge.update(json2)
print("merge: 
", merge)

得到結果如下:

json1:  {‘a‘: ‘1‘, ‘b‘: ‘2‘}
json2:  {‘c‘: ‘4‘, ‘b‘: ‘3‘}
merge:  {‘c‘: ‘4‘, ‘a‘: ‘1‘, ‘b‘: ‘3‘}

python3兩個字典的合並