区块链技术博客
www.b2bchain.cn

JSON打印从根到叶的所有路径 – python程序员分享

本文介绍了JSON打印从根到叶的所有路径 – python程序员分享,有助于帮助完成毕业设计以及求职,是一篇很好的资料。

对技术面试,学习经验等有一些体会,在此分享。

[         {             "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": []         } ] 

想要打印从根到叶的所有路径(一个带有空“子代”的路径)。例如Basic.DeviceInfo.DeviceInfo Form.DeviceInfo Form1

一切正常,直到DeviceInfo Form2

当涉及到DeviceInfo表时,就会出现DeviceInfo表单-> 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) 

python大神给出的解决方案

您在else子句中设置path = path +dic['name']+'.'。一旦walk()函数完成遍历DeviceInfoForm的“子级”,它将尝试遍历DeviceInfoTable。但是,您的函数已经将路径设置为
Basic.DeviceInfo.DeviceInfoForm.

您需要重新组织功能,以免在else:语句中设置路径。也许

else:     walk(dic['childrens'], path+dic['name']+'.') 

这样,您会将正确的路径传递给函数,但未显式设置它。

赞(0) 打赏
部分文章转自网络,侵权联系删除b2bchain区块链学习技术社区 » JSON打印从根到叶的所有路径 – python程序员分享
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

b2b链

联系我们联系我们