我正在尝试在python中编写嵌套类型的字典。我提供了我的输入和预期的输出以及我尝试过的代码。
这是我的输入内容
input = [['10','PS_S1U_X2_LP','permit','origin','igp','RM_S1U_X2_LP'],['20','',['10','MPLS-LOOPBACK','MPLS-LOOPBACK-RLFA'],]
我想要的输出是:
output =
"route_policy_list": [
{
"policy_terms": [],"route_policy_statement": [
{
"entry": "10","prefix_list": "PS_S1U_X2_LP","action_statements": [
{
"action_value": "igp","action": "permit","action_statement": "origin"
}
]
},{
"entry": "20","prefix_list": "","action_statements": [
{
"action_value": "","action_statement": ""
}
]
}
],"name": "RM_S1U_X2_LP"
},{
"policy_terms": [],"prefix_list": "MPLS-LOOPBACK","action_statements": [
{
"action_value": "","action_statement": ""
}
]
}
],"name": "MPLS-LOOPBACK-RLFA"
}
]
我已经尝试过这段代码:
from collections import defaultdict
res1 = defaultdict(list)
for fsm1 in input:
name1 = fsm1.pop()
action = fsm1[2]
action_statement = fsm1[3]
action_value = fsm1[4]
item1 = dict(zip(['entry','prefix_list'],fsm1))
res1['action'] = action
res1['action_statement'] = action_statement
res1['action_value'] = action_value
res1[name].append(item1)
print(res1)
由于我是编码和写作的新手,请帮助我获得上述期望的输出。