我有一列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]
但是我只有一本字典只得到最后一行,我希望每一行
答案 0 :(得分:1)
这是我在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'}
希望这会有所帮助!
答案 1 :(得分:0)
似乎像this的答案应该有所帮助(尽管不确定,因为您没有提供所需的输出):
d = {}
for i in l:
for k in i.keys():
d[k] = list(d[k] for d in l)
{'createdAt': ['2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z', '2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z'], 'notes': ['Candidate initial submission.', '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>', '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>', 'Candidate initial submission.', '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>', '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': ['Steven Klinger', 'Matt', 'Matt', 'Steven Klinger', 'Matt', 'Matt']}
答案 2 :(得分:0)
这使数据通过了一遍:
from collections import defaultdict
output_dict = defaultdict(list)
for d in l:
for key in d:
output_dict[key].append(d[key])
>>> output
defaultdict(list,
{'createdAt': ['2018-12-18T16:09:57.098Z',
'2018-12-18T23:14:09.415Z',
'2019-01-22T16:04:46.958Z',
'2018-12-18T16:09:57.098Z',
'2018-12-18T23:14:09.415Z',
'2019-01-22T16:04:46.958Z'],
'notes': ['Candidate initial submission.',
'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>',
'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>',
'Candidate initial submission.',
'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>',
'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': ['Steven Klinger',
'Matt',
'Matt',
'Steven Klinger',
'Matt',
'Matt']})
答案 3 :(得分:0)
原始答案
我假设您需要一个键,并且该键的所有值都将附加在列表中。在这里,我使用了setdefault
的{{1}}方法来实现它。
dictionary
修改后的答案
# Input
l=[{'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'}]
# Main code
res = {} # defined output dict
for i in l: # for loop to fetch each element(dict) inside a list
for k, v in i.items(): # to fetch key value fair of each dict
res.setdefault(k, []).append(v) # setdefault method of add key to result and created an empty list and appended value to it.
print (res) # print result
# Output
# {'createdAt': ['2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z', '2018-12-18T16:09:57.098Z', '2018-12-18T23:14:09.415Z', '2019-01-22T16:04:46.958Z'], 'notes': ['Candidate initial submission.', '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>', '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>', 'Candidate initial submission.', '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>', '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': ['Steven Klinger', 'Matt', 'Matt', 'Steven Klinger', 'Matt', 'Matt']}
我希望这对您有所帮助!