首页 > 解决方案 > 添加两个已经在同一个列表中的字典

问题描述

我有一列 50983 行。每行都有一个列表,其中有两个或多个字典。我想在一本字典中制作所有字典。我想在每个词典中更新这个 id。我用了 :

l=[{'id':'abc12vr'},{'createdAt': '2018-12-18T16:09:57.098Z',
  'notes': 'Candidate initial submission.',
  'createdBy': 'Steven Klinger'},
 {'createdAt': '2018-12-18T23:14:09.415Z',
  'notes': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
  'createdBy': 'Matt'},
 {'createdAt': '2019-01-22T16:04:46.958Z',
  'notes': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
  'createdBy': 'Matt'},
 {'createdAt': '2018-12-18T16:09:57.098Z',
  'notes': 'Candidate initial submission.',
  'createdBy': 'Steven Klinger'},
 {'createdAt': '2018-12-18T23:14:09.415Z',
  'notes': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>',
  'createdBy': 'Matt'},
 {'createdAt': '2019-01-22T16:04:46.958Z',
  'notes': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>',
  'createdBy': 'Matt'}]

id_dict = [d for d in l if 'id' in d][0]
merge = [{**d,**id_dict} for d in l if 'id' not in d]

但是我只得到最后一行的字典,我想要每一行

标签: pythonpandaslistdictionary

解决方案


这是我在 stackflow 中的首次回答,希望对您有所帮助!

你只得到一个字典的最后一行,我想要每一行 - 因为字典必须有一个唯一的键,而且字典中的所有键都是相同的,这就是 python 不断覆盖键的地方。

下面的代码确实会将所有字典合并为一个,并将键附加一个计数器值以使键唯一。

merged_dict={}
counter=0
def merge_logic(dict_para):
    #print dict_val
    global counter
    for key,value in dict_para.items():    
        merged_dict[key+"_"+str(counter)]=value
        counter+=1
id_dict = [merge_logic(d) for d in l if isinstance(d,dict)]

print merged_dict

输出:

    {'createdAt_11': '2018-12-18T16:09:57.098Z', 
'notes_0': 'Candidate initial submission.', 
'notes_3': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 
'createdAt_14': '2018-12-18T23:14:09.415Z', 
'createdAt_17': '2019-01-22T16:04:46.958Z', 
'notes_6': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>', 
'notes_9': 'Candidate initial submission.', 
'createdBy_13': 'Matt', 
'notes_12': 'The Candidate Status has now been updated from <strong>CV Submitted</strong> and <strong>Feedback Pending</strong> to <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong>', 
'createdAt_5': '2018-12-18T23:14:09.415Z', 
'notes_15': 'The Candidate Status has now been updated from <strong>Client CV Review</strong> and <strong>Feedback Awaiting</strong> to <strong>Client CV Review</strong> and <strong>Position on Hold</strong>', 
'createdAt_2': '2018-12-18T16:09:57.098Z', 
'createdBy_4': 'Matt', 
'createdBy_7': 'Matt', 
'createdBy_1': 'Steven Klinger', 
'createdAt_8': '2019-01-22T16:04:46.958Z', 
'createdBy_10': 'Steven Klinger', 
'createdBy_16': 'Matt'}

希望这可以帮助!


推荐阅读