python 实现服务树结构化

1.  所有服务树数据

tree_list = [{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0},
             {'id': 2, 'pid': 1, 'name': 'a1', 'path': '1211.a1', 'leaf': 0, 'type': 0},
             {'id': 3, 'pid': 1, 'name': 'a2', 'path': '1211.a2', 'leaf': 1, 'type': 0},
             {'id': 16, 'pid': 0, 'name': 'ddssa', 'path': 'ddssa', 'leaf': 0, 'type': 0},
             {'id': 17, 'pid': 16, 'name': '11', 'path': 'ddssa.11', 'leaf': 0, 'type': 0},
             {'id': 18, 'pid': 17, 'name': '121ss1', 'path': 'ddssa.11.121ss1', 'leaf': 1, 'type': 0},
             {'id': 19, 'pid': 17, 'name': '13', 'path': 'ddssa.11.13', 'leaf': 1, 'type': 0},
             {'id': 22, 'pid': 17, 'name': 'tesee', 'path': 'ddssa.11.tesee', 'leaf': 1, 'type': 0},
             {'id': 28, 'pid': 0, 'name': 'system', 'path': 'system', 'leaf': 0, 'type': 0},
             {'id': 29, 'pid': 28, 'name': 'openstack', 'path': 'system.openstack', 'leaf': 0, 'type': 0},
             {'id': 30, 'pid': 28, 'name': 'dstack', 'path': 'system.dstack', 'leaf': 0, 'type': 0},
             {'id': 31, 'pid': 28, 'name': 'aws', 'path': 'system.aws', 'leaf': 0, 'type': 0},
             {'id': 32, 'pid': 17, 'name': 'tese11e', 'path': 'ddssa.11.tese11e', 'leaf': 1, 'type': 0},
             {'id': 33, 'pid': 0, 'name': 'coremap', 'path': 'coremap', 'leaf': 0, 'type': 0},
             {'id': 37, 'pid': 33, 'name': 'mysql', 'path': 'coremap.mysql', 'leaf': 0, 'type': 0},
             {'id': 38, 'pid': 0, 'name': 'cloud', 'path': 'cloud', 'leaf': 0, 'type': 0},
             {'id': 39, 'pid': 37, 'name': '3306', 'path': 'coremap.mysql.3306', 'leaf': 1, 'type': 0},
             {'id': 40, 'pid': 33, 'name': 'redis', 'path': 'coremap.redis', 'leaf': 0, 'type': 0},
             {'id': 41, 'pid': 40, 'name': '6379', 'path': 'coremap.redis.6379', 'leaf': 1, 'type': 0},
             {'id': 42, 'pid': 38, 'name': 'dbs', 'path': 'cloud.dbs', 'leaf': 1, 'type': 0},
             {'id': 51, 'pid': 0, 'name': 'ceshi001', 'path': 'ceshi001', 'leaf': 0, 'type': 0},
             {'id': 61, 'pid': 51, 'name': '110', 'path': 'ceshi001.110', 'leaf': 0, 'type': 0},
             {'id': 62, 'pid': 61, 'name': '62', 'path': 'ceshi001.110.62', 'leaf': 1, 'type': 0},
             {'id': 63, 'pid': 0, 'name': 'imp', 'path': 'imp', 'leaf': 0, 'type': 0},
             {'id': 64, 'pid': 0, 'name': 'test', 'path': 'test', 'leaf': 0, 'type': 0},
             {'id': 73, 'pid': 0, 'name': 'cheshi001', 'path': 'cheshi001', 'leaf': 0, 'type': 0},
             {'id': 74, 'pid': 73, 'name': 'aaa', 'path': 'cheshi001.aaa', 'leaf': 0, 'type': 0},
             {'id': 76, 'pid': 74, 'name': 'bbb', 'path': 'cheshi001.aaa.bbb', 'leaf': 1, 'type': 0},
             {'id': 77, 'pid': 73, 'name': 'ccc', 'path': 'cheshi001.ccc', 'leaf': 0, 'type': 0},
             {'id': 79, 'pid': 77, 'name': 'eee', 'path': 'cheshi001.ccc.eee', 'leaf': 1, 'type': 0},
             {'id': 80, 'pid': 51, 'name': 'nginx', 'path': 'ceshi001.nginx', 'leaf': 0, 'type': 0},
             {'id': 81, 'pid': 80, 'name': 'lb', 'path': 'ceshi001.nginx.lb', 'leaf': 0, 'type': 0},
             {'id': 82, 'pid': 81, 'name': '443', 'path': 'ceshi001.nginx.lb.443', 'leaf': 1, 'type': 0}]

2. 实现需求

### 要实现的工能思路
# 1. pid表示是当前的数据的父级节点
# 2. 如果当前数据的pid和所有的服务树数据的id相等表示,该数据是pid对应数据的子节点加入到children列表中
# {'id': 33, 'pid': 0, 'name': 'coremap', 'path': 'coremap', 'leaf': 0, 'type': 0, 'children': [{'id': 37, 'pid': 33, 'name': 'mysql', 'path': 'coremap.mysql', 'leaf': 0, 'type': 0, 'children': [{'id': 39, 'pid': 37, 'name': '3306', 'path': 'coremap.mysql.3306', 'leaf': 1, 'type': 0}]}, {'id': 40, 'pid': 33, 'name': 'redis', 'path': 'coremap.redis', 'leaf': 0, 'type': 0, 'children': [{'id': 41, 'pid': 40, 'name': '6379', 'path': 'coremap.redis.6379', 'leaf': 1, 'type': 0}]}]}

 3. 代码剖析

### 先以pid做倒叙降序排序
sort_tree_list = sorted(tree_list, key=lambda e: e.__getitem__('pid'),reverse = True)
for st in sort_tree_list:
    print(st)
"""
{'id': 82, 'pid': 81, 'name': '443', 'path': 'ceshi001.nginx.lb.443', 'leaf': 1, 'type': 0}
{'id': 81, 'pid': 80, 'name': 'lb', 'path': 'ceshi001.nginx.lb', 'leaf': 0, 'type': 0}
{'id': 79, 'pid': 77, 'name': 'eee', 'path': 'cheshi001.ccc.eee', 'leaf': 1, 'type': 0}
{'id': 76, 'pid': 74, 'name': 'bbb', 'path': 'cheshi001.aaa.bbb', 'leaf': 1, 'type': 0}
{'id': 74, 'pid': 73, 'name': 'aaa', 'path': 'cheshi001.aaa', 'leaf': 0, 'type': 0}
{'id': 77, 'pid': 73, 'name': 'ccc', 'path': 'cheshi001.ccc', 'leaf': 0, 'type': 0}
{'id': 62, 'pid': 61, 'name': '62', 'path': 'ceshi001.110.62', 'leaf': 1, 'type': 0}
{'id': 61, 'pid': 51, 'name': '110', 'path': 'ceshi001.110', 'leaf': 0, 'type': 0}
{'id': 80, 'pid': 51, 'name': 'nginx', 'path': 'ceshi001.nginx', 'leaf': 0, 'type': 0}
{'id': 41, 'pid': 40, 'name': '6379', 'path': 'coremap.redis.6379', 'leaf': 1, 'type': 0}
{'id': 42, 'pid': 38, 'name': 'dbs', 'path': 'cloud.dbs', 'leaf': 1, 'type': 0}
{'id': 39, 'pid': 37, 'name': '3306', 'path': 'coremap.mysql.3306', 'leaf': 1, 'type': 0}
{'id': 37, 'pid': 33, 'name': 'mysql', 'path': 'coremap.mysql', 'leaf': 0, 'type': 0}
{'id': 40, 'pid': 33, 'name': 'redis', 'path': 'coremap.redis', 'leaf': 0, 'type': 0}
{'id': 29, 'pid': 28, 'name': 'openstack', 'path': 'system.openstack', 'leaf': 0, 'type': 0}
{'id': 30, 'pid': 28, 'name': 'dstack', 'path': 'system.dstack', 'leaf': 0, 'type': 0}
{'id': 31, 'pid': 28, 'name': 'aws', 'path': 'system.aws', 'leaf': 0, 'type': 0}
{'id': 18, 'pid': 17, 'name': '121ss1', 'path': 'ddssa.11.121ss1', 'leaf': 1, 'type': 0}
{'id': 19, 'pid': 17, 'name': '13', 'path': 'ddssa.11.13', 'leaf': 1, 'type': 0}
{'id': 22, 'pid': 17, 'name': 'tesee', 'path': 'ddssa.11.tesee', 'leaf': 1, 'type': 0}
{'id': 32, 'pid': 17, 'name': 'tese11e', 'path': 'ddssa.11.tese11e', 'leaf': 1, 'type': 0}
{'id': 17, 'pid': 16, 'name': '11', 'path': 'ddssa.11', 'leaf': 0, 'type': 0}
{'id': 2, 'pid': 1, 'name': 'a1', 'path': '1211.a1', 'leaf': 0, 'type': 0}
{'id': 3, 'pid': 1, 'name': 'a2', 'path': '1211.a2', 'leaf': 1, 'type': 0}
{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0}
{'id': 16, 'pid': 0, 'name': 'ddssa', 'path': 'ddssa', 'leaf': 0, 'type': 0}
{'id': 28, 'pid': 0, 'name': 'system', 'path': 'system', 'leaf': 0, 'type': 0}
{'id': 33, 'pid': 0, 'name': 'coremap', 'path': 'coremap', 'leaf': 0, 'type': 0}
{'id': 38, 'pid': 0, 'name': 'cloud', 'path': 'cloud', 'leaf': 0, 'type': 0}
{'id': 51, 'pid': 0, 'name': 'ceshi001', 'path': 'ceshi001', 'leaf': 0, 'type': 0}
{'id': 63, 'pid': 0, 'name': 'imp', 'path': 'imp', 'leaf': 0, 'type': 0}
{'id': 64, 'pid': 0, 'name': 'test', 'path': 'test', 'leaf': 0, 'type': 0}
{'id': 73, 'pid': 0, 'name': 'cheshi001', 'path': 'cheshi001', 'leaf': 0, 'type': 0}
"""

4.  最后处理逻辑

### 从上往下将子节点往父级节点的children列表中追加
for tree in sort_tree_list:
    for tree_s in tree_list:
        if tree['pid'] == tree_s['id']:
            if tree_s.get('children',''):
                tree_s['children'].append(tree)
            else:
                tree_s['children'] = []
                tree_s['children'].append(tree)
            tree_list.remove(tree)

 5. 结果

for s in tree_list:
    print(s)

"""
{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0, 'children': [{'id': 2, 'pid': 1, 'name': 'a1', 'path': '1211.a1', 'leaf': 0, 'type': 0}, {'id': 3, 'pid': 1, 'name': 'a2', 'path': '1211.a2', 'leaf': 1, 'type': 0}]}
{'id': 16, 'pid': 0, 'name': 'ddssa', 'path': 'ddssa', 'leaf': 0, 'type': 0, 'children': [{'id': 17, 'pid': 16, 'name': '11', 'path': 'ddssa.11', 'leaf': 0, 'type': 0, 'children': [{'id': 18, 'pid': 17, 'name': '121ss1', 'path': 'ddssa.11.121ss1', 'leaf': 1, 'type': 0}, {'id': 19, 'pid': 17, 'name': '13', 'path': 'ddssa.11.13', 'leaf': 1, 'type': 0}, {'id': 22, 'pid': 17, 'name': 'tesee', 'path': 'ddssa.11.tesee', 'leaf': 1, 'type': 0}, {'id': 32, 'pid': 17, 'name': 'tese11e', 'path': 'ddssa.11.tese11e', 'leaf': 1, 'type': 0}]}]}
{'id': 28, 'pid': 0, 'name': 'system', 'path': 'system', 'leaf': 0, 'type': 0, 'children': [{'id': 29, 'pid': 28, 'name': 'openstack', 'path': 'system.openstack', 'leaf': 0, 'type': 0}, {'id': 30, 'pid': 28, 'name': 'dstack', 'path': 'system.dstack', 'leaf': 0, 'type': 0}, {'id': 31, 'pid': 28, 'name': 'aws', 'path': 'system.aws', 'leaf': 0, 'type': 0}]}
{'id': 33, 'pid': 0, 'name': 'coremap', 'path': 'coremap', 'leaf': 0, 'type': 0, 'children': [{'id': 37, 'pid': 33, 'name': 'mysql', 'path': 'coremap.mysql', 'leaf': 0, 'type': 0, 'children': [{'id': 39, 'pid': 37, 'name': '3306', 'path': 'coremap.mysql.3306', 'leaf': 1, 'type': 0}]}, {'id': 40, 'pid': 33, 'name': 'redis', 'path': 'coremap.redis', 'leaf': 0, 'type': 0, 'children': [{'id': 41, 'pid': 40, 'name': '6379', 'path': 'coremap.redis.6379', 'leaf': 1, 'type': 0}]}]}
{'id': 38, 'pid': 0, 'name': 'cloud', 'path': 'cloud', 'leaf': 0, 'type': 0, 'children': [{'id': 42, 'pid': 38, 'name': 'dbs', 'path': 'cloud.dbs', 'leaf': 1, 'type': 0}]}
{'id': 51, 'pid': 0, 'name': 'ceshi001', 'path': 'ceshi001', 'leaf': 0, 'type': 0, 'children': [{'id': 61, 'pid': 51, 'name': '110', 'path': 'ceshi001.110', 'leaf': 0, 'type': 0, 'children': [{'id': 62, 'pid': 61, 'name': '62', 'path': 'ceshi001.110.62', 'leaf': 1, 'type': 0}]}, {'id': 80, 'pid': 51, 'name': 'nginx', 'path': 'ceshi001.nginx', 'leaf': 0, 'type': 0, 'children': [{'id': 81, 'pid': 80, 'name': 'lb', 'path': 'ceshi001.nginx.lb', 'leaf': 0, 'type': 0, 'children': [{'id': 82, 'pid': 81, 'name': '443', 'path': 'ceshi001.nginx.lb.443', 'leaf': 1, 'type': 0}]}]}]}
{'id': 63, 'pid': 0, 'name': 'imp', 'path': 'imp', 'leaf': 0, 'type': 0}
{'id': 64, 'pid': 0, 'name': 'test', 'path': 'test', 'leaf': 0, 'type': 0}
{'id': 73, 'pid': 0, 'name': 'cheshi001', 'path': 'cheshi001', 'leaf': 0, 'type': 0, 'children': [{'id': 74, 'pid': 73, 'name': 'aaa', 'path': 'cheshi001.aaa', 'leaf': 0, 'type': 0, 'children': [{'id': 76, 'pid': 74, 'name': 'bbb', 'path': 'cheshi001.aaa.bbb', 'leaf': 1, 'type': 0}]}, {'id': 77, 'pid': 73, 'name': 'ccc', 'path': 'cheshi001.ccc', 'leaf': 0, 'type': 0, 'children': [{'id': 79, 'pid': 77, 'name': 'eee', 'path': 'cheshi001.ccc.eee', 'leaf': 1, 'type': 0}]}]}
"""

6. 完整代码

tree_list = [{'id': 1, 'pid': 0, 'name': '1211', 'path': '1211', 'leaf': 0, 'type': 0},
             {'id': 2, 'pid': 1, 'name': 'a1', 'path': '1211.a1', 'leaf': 0, 'type': 0},
             {'id': 3, 'pid': 1, 'name': 'a2', 'path': '1211.a2', 'leaf': 1, 'type': 0},
             {'id': 16, 'pid': 0, 'name': 'ddssa', 'path': 'ddssa', 'leaf': 0, 'type': 0},
             {'id': 17, 'pid': 16, 'name': '11', 'path': 'ddssa.11', 'leaf': 0, 'type': 0},
             {'id': 18, 'pid': 17, 'name': '121ss1', 'path': 'ddssa.11.121ss1', 'leaf': 1, 'type': 0},
             {'id': 19, 'pid': 17, 'name': '13', 'path': 'ddssa.11.13', 'leaf': 1, 'type': 0},
             {'id': 22, 'pid': 17, 'name': 'tesee', 'path': 'ddssa.11.tesee', 'leaf': 1, 'type': 0},
             {'id': 28, 'pid': 0, 'name': 'system', 'path': 'system', 'leaf': 0, 'type': 0},
             {'id': 29, 'pid': 28, 'name': 'openstack', 'path': 'system.openstack', 'leaf': 0, 'type': 0},
             {'id': 30, 'pid': 28, 'name': 'dstack', 'path': 'system.dstack', 'leaf': 0, 'type': 0},
             {'id': 31, 'pid': 28, 'name': 'aws', 'path': 'system.aws', 'leaf': 0, 'type': 0},
             {'id': 32, 'pid': 17, 'name': 'tese11e', 'path': 'ddssa.11.tese11e', 'leaf': 1, 'type': 0},
             {'id': 33, 'pid': 0, 'name': 'coremap', 'path': 'coremap', 'leaf': 0, 'type': 0},
             {'id': 37, 'pid': 33, 'name': 'mysql', 'path': 'coremap.mysql', 'leaf': 0, 'type': 0},
             {'id': 38, 'pid': 0, 'name': 'cloud', 'path': 'cloud', 'leaf': 0, 'type': 0},
             {'id': 39, 'pid': 37, 'name': '3306', 'path': 'coremap.mysql.3306', 'leaf': 1, 'type': 0},
             {'id': 40, 'pid': 33, 'name': 'redis', 'path': 'coremap.redis', 'leaf': 0, 'type': 0},
             {'id': 41, 'pid': 40, 'name': '6379', 'path': 'coremap.redis.6379', 'leaf': 1, 'type': 0},
             {'id': 42, 'pid': 38, 'name': 'dbs', 'path': 'cloud.dbs', 'leaf': 1, 'type': 0},
             {'id': 51, 'pid': 0, 'name': 'ceshi001', 'path': 'ceshi001', 'leaf': 0, 'type': 0},
             {'id': 61, 'pid': 51, 'name': '110', 'path': 'ceshi001.110', 'leaf': 0, 'type': 0},
             {'id': 62, 'pid': 61, 'name': '62', 'path': 'ceshi001.110.62', 'leaf': 1, 'type': 0},
             {'id': 63, 'pid': 0, 'name': 'imp', 'path': 'imp', 'leaf': 0, 'type': 0},
             {'id': 64, 'pid': 0, 'name': 'test', 'path': 'test', 'leaf': 0, 'type': 0},
             {'id': 73, 'pid': 0, 'name': 'cheshi001', 'path': 'cheshi001', 'leaf': 0, 'type': 0},
             {'id': 74, 'pid': 73, 'name': 'aaa', 'path': 'cheshi001.aaa', 'leaf': 0, 'type': 0},
             {'id': 76, 'pid': 74, 'name': 'bbb', 'path': 'cheshi001.aaa.bbb', 'leaf': 1, 'type': 0},
             {'id': 77, 'pid': 73, 'name': 'ccc', 'path': 'cheshi001.ccc', 'leaf': 0, 'type': 0},
             {'id': 79, 'pid': 77, 'name': 'eee', 'path': 'cheshi001.ccc.eee', 'leaf': 1, 'type': 0},
             {'id': 80, 'pid': 51, 'name': 'nginx', 'path': 'ceshi001.nginx', 'leaf': 0, 'type': 0},
             {'id': 81, 'pid': 80, 'name': 'lb', 'path': 'ceshi001.nginx.lb', 'leaf': 0, 'type': 0},
             {'id': 82, 'pid': 81, 'name': '443', 'path': 'ceshi001.nginx.lb.443', 'leaf': 1, 'type': 0}]

### 要实现的工能
# 1. pid表示是当前的数据的父级节点
# 2. 如果当前数据的pid和所有的服务树数据的id相等表示,该数据是pid对应数据的子节点加入到children列表中
# {'id': 33, 'pid': 0, 'name': 'coremap', 'path': 'coremap', 'leaf': 0, 'type': 0, 'children': [{'id': 37, 'pid': 33, 'name': 'mysql', 'path': 'coremap.mysql', 'leaf': 0, 'type': 0, 'children': [{'id': 39, 'pid': 37, 'name': '3306', 'path': 'coremap.mysql.3306', 'leaf': 1, 'type': 0}]}, {'id': 40, 'pid': 33, 'name': 'redis', 'path': 'coremap.redis', 'leaf': 0, 'type': 0, 'children': [{'id': 41, 'pid': 40, 'name': '6379', 'path': 'coremap.redis.6379', 'leaf': 1, 'type': 0}]}]}


### 实现思路
### 先以pid做倒叙降序排序
sort_tree_list = sorted(tree_list, key=lambda e: e.__getitem__('pid'),reverse = True)

### 从上往下将子节点往父级节点的children列表中追加
for tree in sort_tree_list:
    for tree_s in tree_list:
        if tree['pid'] == tree_s['id']:
            if tree_s.get('children',''):
                tree_s['children'].append(tree)
            else:
                tree_s['children'] = []
                tree_s['children'].append(tree)
            tree_list.remove(tree)

 7. 前端实现效果

原文地址:https://www.cnblogs.com/supery007/p/11721922.html