加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 父子节点生成字典

发布时间:2020-12-20 10:20:52 所属栏目:Python 来源:网络整理
导读:? lines = [(1,1, ‘ 父1节点 ‘ ),(2, ‘ 1-2 ‘ ),(3, ‘ 1-3 ‘ ),(4,3, ‘ 1-3-4 ‘ ),(5, ‘ 1-3-5 ‘ ),(6, ‘ 1-3-6 ‘ ),( 7,7, ‘ 父7节点 ‘ ),(8, ‘ 7-8 ‘ ),(9, ‘ 7-9 ‘ )]nodes = {}data_temp = [] for line in lines: id,parentId,name =

?

lines = [(1,1,父1节点),(2,1-2),(3,1-3),(4,3,1-3-4),(5,1-3-5),(6,1-3-6),(7,7,父7节点),(8,7-8),(9,7-9)]

nodes = {}
data_temp =[]
for line in lines:
    id,parentId,name = line
    nodes[id] = {children: [],id: id,"parentId": parentId,"name": name,orLeafnode: 1} # orLeafnode 是叶子节点
    data_temp.append({children: [],orLeafnode: 1})
data = []
for i in data_temp:
    id = i[id]
    parent_id = i[parentId]
    node = nodes[id]
    if id == parent_id:
        node[orLeafnode] = 0
        data.append(node)
    else:
        parent = nodes[parent_id]
        parent[orLeafnode] = 0
        parent[children].append(node)
print(data)
参考: http://www.cocoachina.com/articles/69831

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读