python - 添加两个已经在同一个列表中的字典
问题描述
我有一列 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]
但是我只得到最后一行的字典,我想要每一行
解决方案
这是我在 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'}
希望这可以帮助!
推荐阅读
- python - Python Tkinter 编辑菜单窗口
- ruby-on-rails - Rails:为什么我在尝试使用嵌套属性时会收到错误“未定义的方法 `model_name'”?
- java - 为什么我的代码会出现 ClassNotFoundException?
- java - 调用 java.sql.DriverManager.getConnection() 时不同方法的 NoSuchMethodError
- android - 在 android 上检测蓝牙关闭的 altbeacon
- go - Go defer 函数返回值
- python - 如何使用 Numpy 在文本文件的三列中写入三个不同的值(x、y、z)?
- python - 如何使用 flask-sqlalchemy 查询博客文章的类别?
- java - 如何为所有 JUnit 测试创建对象?
- python - 通过重新采样每一行来扩展数据框,使其在熊猫中每个特定行之前和之后都有一个范围