美好的一天,我在python中有这个问题,我想合并字典的字典列表.. 这是我想要合并的东西:
a = {
"0": {
"Holder": "23002154-1",
"HolderJob": "243340545",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "5",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613"
},
"1": {
"Holder": "23002158-1",
"HolderJob": "243340544",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "9",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613"
}
}
b = {
"0": {
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
},
"1": {
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
}
}
示例输出必须是:
merge_a_b = {
"0": {
"Holder": "23002154-1",
"HolderJob": "243340545",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "5",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613",
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
},
"1": {
"Holder": "23002158-1",
"HolderJob": "243340544",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "9",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613",
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
}
}
到目前为止,我已经使用了这段代码,但似乎无法运行:
def merge_dict(dict1,dict2):
dictio = dict(dict1,**dict2)
return dictio
答案 0 :(得分:2)
是的,dict(dict1,**dict2)
不起作用,因为它只会使用稍后出现的新值(来自dict2
)覆盖密钥的值。你应该做的是 -
def merge_dict(dict1,dict2):
resdict = {}
for k,v in dict2.items():
resdict[k] = dict(v)
resdict[k].update(dict1.get(k,{})))
return resdict
演示 -
>>> a = {
... "0": {
... "Holder": "23002154-1",
... "HolderJob": "243340545",
... "IsControl": "N",
... "IsSPC": "N",
... "LoadPosition": "5",
... "MeasurementType": "XRF",
... "PalletName": "12",
... "PalletPosition": "1",
... "ProcessToolName": "DLCX01",
... "RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
... "RunNumber": "4613"
... },
... "1": {
... "Holder": "23002158-1",
... "HolderJob": "243340544",
... "IsControl": "N",
... "IsSPC": "N",
... "LoadPosition": "9",
... "MeasurementType": "XRF",
... "PalletName": "12",
... "PalletPosition": "1",
... "ProcessToolName": "DLCX01",
... "RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
... "RunNumber": "4613"
... }
... }
>>>
>>> b = {
... "0": {
... "Claimable": "\"false\"",
... "Experiment": "\"264644\"",
... "HTNum": "\"516\"",
... "HolderType": "\"CARJOB\"",
... "MinorRev": "\"140688\"",
... "Operation": "\"510150 DLCX DEPOSITION\"",
... "ParentHolder": "\"23002158\"",
... "ProductName": "\"AE_T_B\"",
... "WaferEC": "\"140517\""
... },
... "1": {
... "Claimable": "\"false\"",
... "Experiment": "\"264644\"",
... "HTNum": "\"516\"",
... "HolderType": "\"CARJOB\"",
... "MinorRev": "\"140688\"",
... "Operation": "\"510150 DLCX DEPOSITION\"",
... "ParentHolder": "\"23002158\"",
... "ProductName": "\"AE_T_B\"",
... "WaferEC": "\"140517\""
... }
... }
>>> def merge_dict(dict1,dict2):
... resdict = {}
... for k,v in dict2.items():
... resdict[k] = dict(v)
... resdict[k].update(dict1.get(k,{})))
... return resdict
...
>>> merged_a_b = merge_dict(a,b)
>>> import pprint
>>> pprint.pprint(merged_a_b)
{'0': {'Claimable': '"false"',
'Experiment': '"264644"',
'HTNum': '"516"',
'Holder': '23002154-1',
'HolderJob': '243340545',
'HolderType': '"CARJOB"',
'IsControl': 'N',
'IsSPC': 'N',
'LoadPosition': '5',
'MeasurementType': 'XRF',
'MinorRev': '"140688"',
'Operation': '"510150 DLCX DEPOSITION"',
'PalletName': '12',
'PalletPosition': '1',
'ParentHolder': '"23002158"',
'ProcessToolName': 'DLCX01',
'ProductName': '"AE_T_B"',
'RecipeName': 'APC_14A_COC_Al2O3_Fill-TEST',
'RunNumber': '4613',
'WaferEC': '"140517"'},
'1': {'Claimable': '"false"',
'Experiment': '"264644"',
'HTNum': '"516"',
'Holder': '23002158-1',
'HolderJob': '243340544',
'HolderType': '"CARJOB"',
'IsControl': 'N',
'IsSPC': 'N',
'LoadPosition': '9',
'MeasurementType': 'XRF',
'MinorRev': '"140688"',
'Operation': '"510150 DLCX DEPOSITION"',
'PalletName': '12',
'PalletPosition': '1',
'ParentHolder': '"23002158"',
'ProcessToolName': 'DLCX01',
'ProductName': '"AE_T_B"',
'RecipeName': 'APC_14A_COC_Al2O3_Fill-TEST',
'RunNumber': '4613',
'WaferEC': '"140517"'}}