First I need to record this code, because I have encountered this problem, and then did not solve
Later, the previous paragraph said I found a piece of code for me to see, I did not care, and then she realized that she realized, she really realized, I ...
In order to thank her, I played with her for a noon Gobang, and pretended to lose a few
First, the purpose:
Recursive iterations
Look at the data again:
In fact, there is a premise that aid cannot be repeated
FID is the parent ID, which points to the aid
If aid = = FID, the aid that proves FID is a subset
If FID = = 0 proves to be the top
Code:
def aaa (Oldarr, FID): NEWARR= [] forIinchOldarr:ifi["FID"] ==Fid:obj= {"Aid": i["Aid"],"Description": i["Description"]} Child= AAA (Oldarr, i["Aid"]) ifLen (Child) >0: obj[" Child"] =Child newarr.append (obj)returnNEWARRif__name__ = ='__main__': Li= [ {"Aid":5,"FID":1,"Description":"Career Values 4","vid":1,"VariableName":"Standard coefficient"}, {"Aid":1,"FID":3,"Description":"Career Values 1","vid":1}, {"Aid":4,"FID":3,"Description":"Career Values 2","vid":2}, {"Aid":7,"FID":3,"Description":"Career Values 3","vid":3}, {"Aid":2,"FID":0,"Description":"Education","vid":4}, {"Aid":3,"FID":0,"Description":"Time","vid":5},] res= {"dirname":"","Child_dirs": [],"Files": []} print (AAA (LI,0))
Python Recursive comment Tree Chart code