简体   繁体   English

如何将两个字典与同一列表中的不同键合并?

[英]how to merge two dictionaries with different keys that are inside same list?

在此处输入图像描述


[
    {"id": "cef8bab0-8482-4086-bc8f-29bf229f13f6"},
    {
        "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",
    },
]

i have this list.我有这个清单。 inside i have two dictionaries.在里面我有两个字典。 i want to merge both into a single dictionary inside the list.我想将两者合并到列表中的单个字典中。

If you intend to merge the id dict into each dict with the other structure, this works:如果您打算将id dict 合并到具有其他结构的每个 dict 中,则可以使用:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM