使用Python合并并添加两个字典列表

时间:2019-07-05 12:35:02

标签: python python-3.x

我有两个字典列表和一段试图合并它们的代码:

Json_Final =[]
try:            
    for keyIN in JSON1:
        json_data_Merge= {}
        for keyUS in JSON2:                
            if(keyIN['YEAR'] == keyUS['YEAR']) & (keyIN['MONTH'] == keyUS['MONTH'])& (keyIN['Name'] == keyUS['Name']):
                json_data_Merge['YEAR'] = keyIN['YEAR']
                json_data_Merge['MONTH'] = keyIN['MONTH']
                json_data_Merge['Name'] = keyIN['Name']
                json_data_Merge['Total']= int(keyIN['Total']) + int(keyUS['Total'])
        Json_Final.append(json_data_Merge)
    print( Json_Final )
except Exception as e:
    print('MergeException',e)

JSON 1 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 100}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  100}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  200}]

JSON 2 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 400}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  500}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  250}]

预期输出:

[{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 300}, 
 {"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300}, 
 {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 600},
 {"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300}, 
 {"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300}, 
 {"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200}, 
 {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  600}, 
 {"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200}, 
 {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  450}]

我的代码输出:

[{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 300}, 
{"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 600}, 
{"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  300}, 
{"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  450}]

1 个答案:

答案 0 :(得分:3)

这是一种方法。

例如:

JSON_1 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 100}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Apple", "Total": 300}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  100}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  200}]

JSON_2 = [{"YEAR": 2019, "MONTH": 1, "Name": "Apple", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 1, "Name": "Orange", "Total": 300}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Grape", "Total": 400}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Orange", "Total": 300}, 
          {"YEAR": 2019, "MONTH": 2, "Name": "Mango", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Grape", "Total":  500}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Orange", "Total": 200}, 
          {"YEAR": 2019, "MONTH": 3, "Name": "Apple", "Total":  250}]

JSON_2 = {"{}_{}_{}".format(i["YEAR"], i["MONTH"], i["Name"]): i for i in JSON_2}    #Create a dict for easy loopup

for i in JSON_1:
    key = "{}_{}_{}".format(i["YEAR"], i["MONTH"], i["Name"])   #Create key with Year, Month, Name
    if key in JSON_2:                         #Check if item from JSON_1 exist in JSON_2
        JSON_2[key]['Total'] += i["Total"]    #Update Total
    else:
        JSON_2[key] = i                       #Else add new entry. 

print(list(JSON_2.values()))    #Get values.

输出:

[{'MONTH': 1, 'Name': 'Orange', 'Total': 300, 'YEAR': 2019},
 {'MONTH': 2, 'Name': 'Mango', 'Total': 200, 'YEAR': 2019},
 {'MONTH': 3, 'Name': 'Apple', 'Total': 450, 'YEAR': 2019},
 {'MONTH': 1, 'Name': 'Apple', 'Total': 300, 'YEAR': 2019},
 {'MONTH': 3, 'Name': 'Grape', 'Total': 600, 'YEAR': 2019},
 {'MONTH': 3, 'Name': 'Orange', 'Total': 200, 'YEAR': 2019},
 {'MONTH': 2, 'Name': 'Grape', 'Total': 600, 'YEAR': 2019},
 {'MONTH': 2, 'Name': 'Apple', 'Total': 300, 'YEAR': 2019},
 {'MONTH': 2, 'Name': 'Orange', 'Total': 300, 'YEAR': 2019}]