如何合并列表中具有相同元素的字典?

How to combine dictionaries that have the same element inside a list?

我有一个字典列表。一些词典共享相同的元素。

data = [
                 {'New_PCP_Name': 'Jack0',
                  'Member_Name': 'Jack0, 0',
                  'Member_ID': '111',
                  'DOB': '111',
                  'PCP_ID':'111'
                  },
                 {'New_PCP_Name': 'Jack0',
                  'Member_Name': 'Jack00, 00',
                  'Member_ID': '222',
                  'DOB': '222',
                  'PCP_ID':'111'
                  },
                    {'New_PCP_Name': 'Jack1',
                     'Member_Name': 'Jack1, 1',
                     'Member_ID': '333',
                     'DOB': '333',
                     'PCP_ID':'333'
                     },
                    {'New_PCP_Name': 'Jack2',
                     'Member_Name': 'Jack2, 2',
                     'Member_ID': '444',
                     'DOB': '444',
                     'PCP_ID':'444'
                     }
                 ]

我需要以特定格式组合它们。前 2 个字典共享相同的元素 'New_PCP_Name':'Jack0'。所以我想把它们结合起来。最终产品如下。这种格式必须完全像这样,因为我需要在从 Excel 导入的数据的邮件合并中使用这种格式。

data = [
                 {'New_PCP_Name': 'Jack0',
                  'PCP_ID':'111',
                  'Member_Name':[{'Member_Name':'Jack0, 0','Member_ID':'111','DOB':'111'},
                               {'Member_Name': 'Jack00, 00', 'Member_ID': '222', 'DOB': '222'}] 
                  },
                    {'New_PCP_Name': 'Jack1',
                     'Member_Name': 'Jack1, 1',
                     'Member_ID': '333',
                     'DOB': '333',
                     'PCP_ID':'333'
                     },
                    {'New_PCP_Name': 'Jack2',
                     'Member_Name': 'Jack2, 2',
                     'Member_ID': '444',
                     'DOB': '444',
                     'PCP_ID':'444'
                     }
                 ]

我是 python.I 的新人,我尝试过拆分列表、修改词典并将它们重新组合在一起,如下所示。那没有成功。请帮我弄清楚如何重新格式化字典列表。或者有没有办法以特定格式从 Excel 为共享相同单元格值的行导入数据?

data2=[]
for x in range (0,len(data),1):
    print(x)
    print(data[x])
    a = data[x]
    print(a['New_PCP_Name'])
    if x+1<=len(data):
        if data[x]['New_PCP_Name'] == data[x+1]['New_PCP_Name']:
            print('yes')
            data2.append(data[x])
        else:
            print('no')

print('data2=', data2)

一种可能性是使用 itertools.groupby (doc):

data = [
                 {'New_PCP_Name': 'Jack0',
                  'Member_Name': 'Jack0, 0',
                  'Member_ID': '111',
                  'DOB': '111',
                  'PCP_ID':'111'
                  },
                 {'New_PCP_Name': 'Jack0',
                  'Member_Name': 'Jack00, 00',
                  'Member_ID': '222',
                  'DOB': '222',
                  'PCP_ID':'111'
                  },
                    {'New_PCP_Name': 'Jack1',
                     'Member_Name': 'Jack1, 1',
                     'Member_ID': '333',
                     'DOB': '333',
                     'PCP_ID':'333'
                     },
                    {'New_PCP_Name': 'Jack2',
                     'Member_Name': 'Jack2, 2',
                     'Member_ID': '444',
                     'DOB': '444',
                     'PCP_ID':'444'
                     }
                 ]

from itertools import groupby

out = []
for v, g in groupby(sorted(data, key=lambda k: k['New_PCP_Name']), lambda k: (k['New_PCP_Name'], k['PCP_ID'])):
    l = [*g]
    if len(l) == 1:
        out.append(l[0])
    else:
        pcp_id = None
        for i in l:
            del i['New_PCP_Name']
            del i['PCP_ID']
        out.append({'New_PCP_Name': v[0],
                    'PCP_ID': v[1],
                  'Member_Name':l
                  })

from pprint import pprint
pprint(out)

打印:

[{'Member_Name': [{'DOB': '111', 'Member_ID': '111', 'Member_Name': 'Jack0, 0'},
                  {'DOB': '222',
                   'Member_ID': '222',
                   'Member_Name': 'Jack00, 00'}],
  'New_PCP_Name': 'Jack0',
  'PCP_ID': '111'},
 {'DOB': '333',
  'Member_ID': '333',
  'Member_Name': 'Jack1, 1',
  'New_PCP_Name': 'Jack1',
  'PCP_ID': '333'},
 {'DOB': '444',
  'Member_ID': '444',
  'Member_Name': 'Jack2, 2',
  'New_PCP_Name': 'Jack2',
  'PCP_ID': '444'}]

尝试

new_data_dic = {}
for e in data:
    new_name = e['New_PCP_Name']
    if new_name not in new_data_dic:
        new_data_dic[new_name] = e.copy()
    else:
        if type(new_data_dic[new_name]['Member_Name'] == str):
            inner = new_data_dic[new_name].copy()
            del inner['New_PCP_Name']
            del new_data_dic[new_name]['Member_ID']
            del new_data_dic[new_name]['DOB']
            new_data_dic[new_name]['Member_Name'] = [inner]
        inner = e.copy()
        del inner['New_PCP_Name']
        new_data_dic[new_name]['Member_Name'].append(inner)
data2 = list(new_data_dic.values())

解释:'New_PCP_Name'是一个键,所以我创建了一个字典。我将 'Member_Name' 转换为一个列表,以防我看到同一键的另一次出现。在随后的出场中,我可以追加。