python实现:{ "network_scenarios": "sdn", "dataplanes_config": [ { "name": "default", "network_type": "DATAPLANE", "segmentation_type": "vxlan", "subnets": [ { "start": "166.166.16.10", "end": "166.166.16.166", "cidr": "166.166.16.0/24", "gateway": "", "vlan_id": "" }, { "start": "16.16.16.10", "end": "16.16.16.166", "cidr": "16.16.16.0/24", "gateway": "", "vlan_id": "" } ] } ], "host-192-168-103-110": { }, "host-192-168-103-111": { }, "sdn_manage_vswitch": "" }取以上内容中的“”subnets“为值,以“cidr”为键重新组成字典“””
时间: 2023-07-08 10:54:55 浏览: 96
可以使用如下Python代码实现:
```python
data = {
"network_scenarios": "sdn",
"dataplanes_config": [
{
"name": "default",
"network_type": "DATAPLANE",
"segmentation_type": "vxlan",
"subnets": [
{
"start": "166.166.16.10",
"end": "166.166.16.166",
"cidr": "166.166.16.0/24",
"gateway": "",
"vlan_id": ""
},
{
"start": "16.16.16.10",
"end": "16.16.16.166",
"cidr": "16.16.16.0/24",
"gateway": "",
"vlan_id": ""
}
]
}
],
"host-192-168-103-110": {},
"host-192-168-103-111": {},
"sdn_manage_vswitch": ""
}
subnets_dict = {}
for subnet in data["dataplanes_config"][0]["subnets"]:
cidr = subnet["cidr"]
subnets_dict[cidr] = subnet
print(subnets_dict)
```
输出结果为:
```
{'166.166.16.0/24': {'start': '166.166.16.10', 'end': '166.166.16.166', 'cidr': '166.166.16.0/24', 'gateway': '', 'vlan_id': ''}, '16.16.16.0/24': {'start': '16.16.16.10', 'end': '16.16.16.166', 'cidr': '16.16.16.0/24', 'gateway': '', 'vlan_id': ''}}
```
其中,`subnets_dict`即为以`cidr`为键重新组成的字典。
阅读全文