如何在Dictionary Python中合并两个条目



我想把字典的两个项目放在一个实体中各自的键如果idx=…如果键相似,则所有具有匹配键的可比较数据在dict中成为一个项目,其值为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]",
]
}
}
}
}
}

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

  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)

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

相关内容

  • 没有找到相关文章