层级关系 练习

层级关系

# coding: utf-8
"""
# @Time    : 2022/2/4 16:17
# @Author  : Gina Gao
# @File    : 
# @Software: PyCharm
# @Descript:
"""
import  sys,os

db=[{'id':1,'name':'唐艺昕','parent':None,'child':[]},
    {'id':2,'name':'唐艺昕2','parent':None,'child':[]},
    {'id':3,'name':'唐艺昕3','parent':1,'child':[]}]
dict={}

for item in db:
    key=item['name']
    print('key',key)
    print(item)
    dict[key]=item
    print('dict',dict )
result=[]
for item in db:
    pid=item['parent']
    print("pid",pid)
    if not pid:
        result.append(item)
        print('result',result)
    else:
        db[pid]['child'].append(item)
print(db)
for i in db:
    print(i)

  

posted @ 2022-02-04 22:26  yongqi-911  阅读(19)  评论(0编辑  收藏  举报