Home > other >  Who can teach me
Who can teach me

Time:01-21

According to the string matching precision, output hierarchy

CodePudding user response:

 import json 

Jso="'
{" Chinese ": [
{" zhejiang ": [{" hangzhou" : [" west lake ", "xixi wetland"]}]},
{" jiangsu ": [{" suzhou:" [] "sun mountain national forest park"}, {" wuxi ": [" three kingdoms shuihu scenic spot", "lake wetland park"]}]}
]
}
"'
Js=json. Loads (jso)

Def getTree (d, parDeep) :
For k, v in d.i tems () :
LstRes. Append (parDeep + k)
If isinstance (v, a list) :
For I in v:
If isinstance (I, dict) :
LstRes. Append (parDeep + k)
GetTree (I, parDeep + k + ". ")
The else:
LstRes. Append (parDeep + k + ' '+ I)

Def find (lstT strKey) :
For I in lstT:
If i.f ind (strKey) & gt; 1:
The return of I

LstRes=list ()
GetTree (js, ' ')
LstRes=list (set (lstRes))
LstRes. Sort (key=lambda x: len (x))
Result=find (lstRes, 'wuxi)
Print (result)
  • Related