我有一个图表清单:
x = [( router_1 , 0/1 , 0/4 , router_2 ),
( router_1 , 0/2 , 0/4 , router_3 ),
( router_4 , 0/1 , 0/5 , router_5 ),
( router_4 , 0/2 , 0/5 , router_6 )]
目标是构建以下数据:
dict = {"details": [
{
"router_1": {
"connections": [
{
"side_a": {
"device": "router_1",
"interface": "0/47",
},
"side_b": {
"interface": "0/17",
"device": "router_2",
}
},
{
"side_a": {
"device": "router_1",
"interface": "0/48",
},
"side_b": {
"interface": "0/17",
"device": "router_3",
}
}
]
}
},
{
"router_4": {
"connections": [
{
"side_a": {
"device": "router_4",
"interface": "0/47",
},
"side_b": {
"interface": "0/17",
"device": "router_5",
}
},
{
"side_a": {
"device": "router_4",
"interface": "0/48",
},
"side_b": {
"interface": "0/17",
"device": "router_6",
}
}
]
}
}
]
}
请注意:
- the key router_1 created based on the device name under "side_a"
- the key router_4 created based on the device name under "side_a"
I tried below :
import collections
data_dict = collections.defaultdict(list)
result = {}
for item in x:
router_a, router_a_int, router_b_int, router_b = item
result[side_a] = data_dict["connections"].append({"side_a": {
"hostname": router_a,
"interface": router_a_int},
"side_b": {
"interface": router_b_int,
"hostname": router_b
}})
然而, 字典的价值正在显示“None”。 象每个时间的 app一样,使价值变得无所作为,但我通过收集信息来消除这一问题。
请您提供帮助。
谢谢。