我想转换一个像这样的列表表示的树结构......
['access_ctrl_allow',
['description', ['Access Control - Allow']],
['type', ['4']],
['metadata'],
['output', ['0']],
['rule',
['0',
['enabled', ['1']],
['action', ['type', ['accept_conn']]],
['match',
['services',
['0',
['name', ['DHCP']],
['trigger',
['0',
['protocol', ['17']],
['dst', ['start', ['67']], ['end', ['67']]],
['src', ['start', ['67']], ['end', ['68']]]]]]]]]]]
进入Python字典,如下所示:
{'access_ctrl_allow': {'output': '0',
'type': '4',
'description': 'Access Control - Allow',
'rule': {'0': {'action': {'type': 'accept_conn'},
'enabled': '1',
'match': {'services': {'0': {'trigger': {'0': {'src': {'start': '67',
'end': '68'},
'dst': {'start': '67', 'end': '67'},
'protocol': '17'}},
'name': 'DHCP'}}}}},
'metadata': {}}}
我有代码执行它并且似乎产生正确的输出......
def dictify(data):
k, v = data[0], data[1:]
if len(v) == 0:
return {k: {}}
elif len(v) == 1 and len(v[0]) == 1:
return {k: v[0][0]}
else:
new = {}
for datum in v:
new.update(dictify(datum))
return {k: new}
......但感觉很笨重。你能提供任何清理建议吗?特别是,需要取消引用列表中的列表(v[0][0]
),这表明必须有更好的方法。
答案 0 :(得分:0)
如果您愿意接受略有不同的输出,可以使用setdefault
:
import json
d={}
def rec(lst,d):
for i in lst:
if not isinstance(i,list):
child=d.setdefault(i,{})
else:
rec(i,child)
rec(lst,d)
print json.dumps(d,indent=3)
<强>出:强>
{
"access_ctrl_allow": {
"output": {
"0": {}
},
"type": {
"4": {}
},
"description": {
"Access Control - Allow": {}
},
"rule": {
"0": {
"action": {
"type": {
"accept_conn": {}
}
},
"enabled": {
"1": {}
},
"match": {
"services": {
"0": {
"trigger": {
"0": {
"src": {
"start": {
"67": {}
},
"end": {
"68": {}
}
},
"dst": {
"start": {
"67": {}
},
"end": {
"67": {}
}
},
"protocol": {
"17": {}
}
}
},
"name": {
"DHCP": {}
}
}
}
}
}
},
"metadata": {}
}
}