Python 如何将列表排序为层次结构父/子/孙?

Python 如何将列表排序为层次结构父/子/孙?,python,html,Python,Html,将“a”列表按父/子层次结构排序。其中第一项是ID,第二项是描述,第三项是父ID a = [('222', 'Workroom', '111'), ('333', 'Setup Part', '222'), ('444', 'Scale', '222'), ('666', 'Workroom', '000'), ('888', 'Setup Part', '777'), ('777', 'Workroom', '666'), ('555', 'Workroom', '111'), ('111',

将“a”列表按父/子层次结构排序。其中第一项是ID,第二项是描述,第三项是父ID

a = [('222', 'Workroom', '111'),
('333', 'Setup Part', '222'),
('444', 'Scale', '222'),
('666', 'Workroom', '000'),
('888', 'Setup Part', '777'),
('777', 'Workroom', '666'),
('555', 'Workroom', '111'),
('111', 'Workroom', '000'),
('120', 'Workroom', '000'),
('100', 'Workroom', '000'),
('101', 'Workroom', '000'),
('110', 'Workroom', '101'),
('130', 'Workroom', '120')]
期望输出

a = [('100', 'Workroom', '000'),
('101', 'Workroom', '000'),
    ('110', 'Workroom', '101'),
('111', 'Workroom', '000'),
    ('555', 'Workroom', '111'),
    ('222', 'Workroom', '111'),
        ('333', 'Setup Part', '222'),
        ('444', 'Scale', '222'),
('120', 'Workroom', '000'),
    ('130', 'Workroom', '120'),
('666', 'Workroom', '000'),
    ('777', 'Workroom', '666'),
        ('888', 'Setup Part', '777'),]

根据以下人员的正确答案:

您可以尝试:

a = [('222', 'Workroom', '111'),
('333', 'Setup Part', '222'),
('444', 'Scale', '222'),
('666', 'Workroom', '000'),
('888', 'Setup Part', '777'),
('777', 'Workroom', '666'),
('555', 'Workroom', '111'),
('111', 'Workroom', '000'),
('120', 'Workroom', '000'),
('100', 'Workroom', '000'),
('101', 'Workroom', '000'),
('110', 'Workroom', '101'),
('130', 'Workroom', '120')]

# step 1: create all the nodes dictionary
nodes = {}
for i in a:
    id, desc, parent_id = i
    nodes[id] = { 'id': id, 'desc': desc }

# step 2: create trees and parent-child relations
forest = []
for i in a:
    id, desc, parent_id = i
    node = nodes[id]

    # either make the node a new tree or link it to its parent
    if parent_id == '000':
        # start a new tree in the forest
        forest.append(node)
    else:
        # add new_node as child to parent
        parent = nodes[parent_id]
        if not 'children' in parent:
            # ensure parent has a 'children' field
            parent['children'] = []
        children = parent['children']
        children.append(node)

# step 3: simple function to print then with indentation
def print_node(node,level=0):
    print("  "*level, node['id'], node['desc'])
    if node.get('children',False):
        for child in node['children']:
            print_node(child,level=level+1)

for node in forest:
    print_node(node,level=0)
在答案中,我们为所有节点创建一个字典,然后为所有than添加一个包含子节点的子值

注意,我不做任何排序,只是创建林和节点并打印它。如果需要,可以使用函数进行排序


告诉我这是不是你想要的。希望它有帮助……

期望的输出是什么?深度优先?广度优先?完全不同的东西?请查看Python文档中的。a=[('100','Workroom','000'),('101','Workroom','000'),('111','Workroom','000'),('555','Workroom','111'),('222','Workroom','111'),('333','Setup Part','222'),('444','Scale','222'),('110','Workroom','101'),('120','Workroom','000'),('130','Workroom','120'),('666','Workroom','000'),('777','Workroom','666'),('888','Setup Part','777'),]应该
('110','Workroom','101'),
不直接位于
('101','Workroom','000'),