【问题标题】:How to Merge Two Two Items in Dictionary Python如何在字典 Python 中合并两个两个项目
【发布时间】:2022-12-18 15:30:57
【问题描述】:

我想将字典的两项放入相应键中的一个实体中,如果 idx=... 在键中相似,则所有具有匹配键的可比较数据都成为字典中的一项,键为 0、1、... 见下面的演示数据要看懂,我learned thispage 但是我做不到如果你知道怎么做,请帮助我

演示数据:

{
    "system": {
        "camera[idx=0]": {
            "fps": {
                "value": 24,
                "xpath": "/system/camera[idx=0]/fps",
                "index": 3,
                "string": "fps",
                "uniqueID": "f8f90bde-e530-4cc6-b350-3e923d6ab456",
                "editable": True,
                "parent": "system",
                "subParents": ["system", "camera[idx=0]"],
            },
        },
        "motion_detection[idx=0]": {
            "threshold_type": {
                "value": 0,
                "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=0]/threshold_type",
                "index": 5,
                "string": "threshold_type",
                "uniqueID": "5532aebe-501d-4275-ac4d-d6c8baf34d45",
                "editable": True,
                "parent": "system",
                "subParents": [
                    "system",
                    "camera[idx=1]",
                    "ip",
                    "motion_detection[idx=0]",
                ],
            },
        },
        "camera[idx=1]": {
            "vendor_name": {
                "value": "Raspberry",
                "xpath": "/system/camera[idx=1]/vendor_name",
                "index": 3,
                "string": "vendor_name",
                "uniqueID": "6ea8386b-fd11-44c4-88e8-b35b8eff9f43",
                "editable": True,
                "parent": "system",
                "subParents": ["system", "camera[idx=1]"],
            }
        },
        "motion_detection[idx=1]": {
            "threshold_min": {
                "value": 0,
                "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=1]/threshold_min",
                "index": 5,
                "string": "threshold_min",
                "uniqueID": "c8eab5a0-e00a-44e9-8320-09e4c8243505",
                "editable": True,
                "parent": "system",
                "subParents": [
                    "system",
                    "camera[idx=1]",
                    "ip",
                    "motion_detection[idx=1]",
                ],
            },
        },
    }
}

期待数据

{
    "system": {
        "0": {
            "camera": {
                "fps": {
                    "value": 24,
                    "xpath": "/system/camera[idx=0]/fps",
                    "index": 3,
                    "string": "fps",
                    "uniqueID": "f8f90bde-e530-4cc6-b350-3e923d6ab456",
                    "editable": True,
                    "parent": "system",
                    "subParents": ["system", "camera[idx=0]"],
                },
            },
            "motion_detection": {
                "threshold_type": {
                    "value": 0,
                    "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=0]/threshold_type",
                    "index": 5,
                    "string": "threshold_type",
                    "uniqueID": "5532aebe-501d-4275-ac4d-d6c8baf34d45",
                    "editable": True,
                    "parent": "system",
                    "subParents": [
                        "system",
                        "camera[idx=1]",
                        "ip",
                        "motion_detection[idx=0]",
                    ]
                }
            }
        },
        "1": {
            "camera": {
                "vendor_name": {
                    "value": "Raspberry",
                    "xpath": "/system/camera[idx=1]/vendor_name",
                    "index": 3,
                    "string": "vendor_name",
                    "uniqueID": "6ea8386b-fd11-44c4-88e8-b35b8eff9f43",
                    "editable": True,
                    "parent": "system",
                    "subParents": ["system", "camera[idx=1]"]
                }
            },
            "motion_detection": {
                "threshold_min": {
                    "value": 0,
                    "xpath": "/system/camera[idx=1]/ip/motion_detection[idx=1]/threshold_min",
                    "index": 5,
                    "string": "threshold_min",
                    "uniqueID": "c8eab5a0-e00a-44e9-8320-09e4c8243505",
                    "editable": True,
                    "parent": "system",
                    "subParents": [
                        "system",
                        "camera[idx=1]",
                        "ip",
                        "motion_detection[idx=1]",
                    ]
                }
            }
        }
    }
}

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    这是我解决问题的最佳尝试 - 我有效地将问题分解为几个步骤:

    1. 获取您要解析的密钥(例如camera[idx=0])。
    2. 提取前导标签和 idx 的值(在上述情况下,camera0)。

      如果你解决了这两点,剩下的就很容易弄清楚了。


      我依靠一点正则表达式解决了这个问题。这是我的解决方案:

      import re
      
      data = ...  # This is the input data structure you supplied in your example. Omitted for brevity.
      
      outputs = {}
      
      for key, value in data["system"].items():
          label, idx = re.search(r"(.+)[idx=(.+)]", key).groups()
      
          if idx not in outputs:
              outputs[idx] = {}
      
          outputs[idx][label] = {**value, **value}
      
      outputs = {"system": outputs}  # Restructure according to required spec.
      print(outputs)
      

      这给了我想要的输出。希望有帮助! :)

    【讨论】:

    • 非常感谢兄弟
    猜你喜欢
    • 1970-01-01
    • 2022-11-15
    • 2022-12-26
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多