【发布时间】:2015-02-18 06:14:36
【问题描述】:
[
{
"name": "Basic",
"id": "home",
"childrens": [
{
"name": "Dashboard",
"viewtype": "custom",
"view": "dashboard.html",
"childrens": []
},
{
"name": "DeviceInfo",
"href": "WSettings",
"childrens": [
{
"name": "DeviceInfo Form",
"childrens": [
{
"name": "DeviceInfo Form1",
"viewtype": "xml",
"view": "dinfo",
"childrens": []
},
{
"name": "DeviceInfo Form2",
"viewtype": "xml",
"view": "complexjson",
"childrens": []
}
]
},
{
"name": "DeviceInfo Table",
"childrens": [
{
"name": "DeviceInfo Table1",
"viewtype": "xml",
"view": "dinfotable",
"childrens": []
},
{
"name": "DeviceInfo Table2",
"viewtype": "xml",
"view": "jsontable",
"childrens": []
}
]
}
]
},
{
"name": "Hybrid",
"childrens": [
{
"name": "Table-Form",
"viewtype": "xml",
"view": "hybrid",
"childrens": []
}
]
}
]
},
{
"name": "Advanced",
"id": "profile",
"childrens": []
}
]
想要打印从根到叶的所有路径(一个为空的'childrens')。 例如 Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Form1
在 DeviceInfo Form2
之前一切正常说到DeviceInfo Table,DeviceInfo Form就登场了 --> Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Table.DeviceInfo Table1。
这不应该发生。相反,我需要 Basic.DeviceInfo.DeviceInfo Table.DeviceInfo Table1。
我的代码哪里出了问题。有什么解决办法吗?
def walk(list1, path = ""):
for dic in list1:
#print('about to walk', dic['name'], 'passing path -->', path)
if(len(dic['childrens']) == 0):
print('leaf --->', path+dic['name']+'.')
else:
path = path+dic['name']+'.'
#passing parent name to childreni
walk(dic['childrens'], path)
【问题讨论】: