如何合并字典列表? [英] How to merge list of dictionaries?

查看:22
本文介绍了如何合并字典列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里扩展我的原始问题:如何附加/合并列表字典?.

I'm extending my original question here: How to append/merge list of dictionaries?.

我正在尝试在一个包含列表的字典列表之间合并一些数据.如果匹配,将根据object"和semver"键进行合并.如果匹配相同的值,还会添加到他们给定的部分".鉴于以下数据:

I'm trying to merge some data between a single List of dictionaries that has lists inside. Merging would happen based on the "object" and "semver" key if they matched. Also adding to their given "section" if the same value was matched. Given the following data:

data = [
        {
         "semver":"1.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "add: comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
      {
         "semver":"1.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "add: Second comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
      {
         "semver":"1.0.0",
         "sections":[
            {
               "name":"Fix",
               "messages":[
                  "Comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
      {
         "semver":"2.0.0",
         "sections":[
            {
               "name":"Fix",
               "messages":[
                  "2.0.0 Fix Comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
      {
         "semver":"2.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "2.0.0 Add Comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
      {
         "semver":"2.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "2.0.0 comment for the NewFile"
               ]
            }
         ],
         "object":"NewFile.sh"
      },
]

我想实现这个作为最终结果

I would like to achieve this as a end result

data = [
        {
         "semver":"1.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "add: comment here",
                  "add: Second comment here"
               ]
            },
            {
               "name":"Fix",
               "messages":[
                  "Fix: comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
        {
         "semver":"2.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "2.0.0 Add comment here",
               ]
            },
            {
               "name":"Fix",
               "messages":[
                  "2.0.0 Fix Comment here"
               ]
            }
         ],
         "object":"files.sh"
      },
      {
         "semver":"2.0.0",
         "sections":[
            {
               "name":"Add",
               "messages":[
                  "2.0.0 comment for the NewFile"
               ]
            }
         ],
         "object":"NewFile.sh"
      },

]

代码块

objects = {}  # mapping for object: object_data with sections
sections = defaultdict(list)  # mapping for object: all sections
for d in data:
    print(d["semver"])
    for k, v in list(d.items()):
        if v == d["semver"]:
            try:
                section = d.pop("sections")
                sections[d["object"]].extend(section)
                objects[d["object"]] = d  # populate with object data without sections
            except Exception as e:
                print(e)
                pass

output = []
for object_name, object_data in objects.items():
    object_data["sections"] = sections[object_name]
    output.append(object_data)

到目前为止,我正在遍历 dict 中的每个 k,v 对,但无法解决两个版本之间的匹配问题并附加到特定的 dict在循环中.

So far I am looping through each k,v pair in the dict's but can't wrap my head around matching between the two versions and appending to that specific dict in the loop.

推荐答案

有 2 个应该做的改变:

There are 2 changes that should be done:

  1. objectssections中的键更改为基于objectsemver的组合,由a表示元组.
  2. 添加一个辅助函数来合并section中的消息
  1. changing the keys in objects and sections to be based on combination of object and semver represented by a tuple.
  2. add an auxiliary function to merge the messages in sections

试试这个:

import json  # just for pretty print, you don't have to use it
from collections import defaultdict


def merge_messages(sections):
    d = defaultdict(list)
    for m in sections:
        d[m["name"]].extend(m["messages"])
    return [{"name": k, "messages": v} for k, v in d.items()]


objects = {}  # mapping for data with sections for (object, semver) combinations
sections = defaultdict(list)  # mapping for sections data for (object, semver) combinations
for d in data:
    section = d.pop("sections")
    sections[(d["object"], d["semver"])].extend(section)  # extends the sections for the object
    objects[(d["object"], d["semver"])] = d  # # populate with object data without sections

# merge between sections and objects by object key
output = []
for comb, object_data in objects.items():
    object_data["sections"] = merge_messages(sections[comb])
    output.append(object_data)
print(json.dumps(output, indent=4))  # just for pretty print

这篇关于如何合并字典列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆