You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中按idx值合并字典中的对应项

按idx值合并字典条目解决方案

我需要把字典中的条目按键内的idx值合并——当键中的idx=...值相同时,所有匹配该idx的对应数据要整合到字典中以0、1…为键的条目里,尝试过相关方法但未实现,求解决方案。

示例数据

{
    "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]",
                    ]
                }
            }
        }
    }
}

解决方案

可以通过正则提取idx值,遍历原字典完成归类整合,代码如下:

import re
import pprint

def merge_by_idx(original_data):
    result = {"system": {}}
    # 匹配idx后的数字
    idx_regex = re.compile(r'idx=(\d+)')
    
    for key, content in original_data["system"].items():
        # 提取idx值
        idx_match = idx_regex.search(key)
        if idx_match:
            idx = idx_match.group(1)
            # 提取设备名称(去掉[idx=...]部分)
            device = key.split('[')[0]
            # 初始化对应idx的条目
            if idx not in result["system"]:
                result["system"][idx] = {}
            # 将设备内容加入对应idx的字典
            result["system"][idx][device] = content
    
    return result

# 测试示例数据
original_dict = {
    "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]",
                ],
            },
        },
    }
}

# 执行合并并打印结果
merged = merge_by_idx(original_dict)
pprint.pprint(merged)

步骤说明

  1. 正则提取idx:用正则表达式匹配键中的idx=数字部分,提取出用于归类的idx值;
  2. 拆分设备名称:将键按[拆分,得到设备类型(如camera、motion_detection);
  3. 归类整合:遍历原字典的每个条目,将相同idx的设备内容放入结果字典对应键下的子字典中;
  4. 输出结果:用pprint模块格式化输出,保证结构清晰。

内容的提问来源于stack exchange,提问作者Bugsfounder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 03:46:13