【问题标题】:How to sort a list into a hierarchy structure parent/child/grandchild?如何将列表排序为层次结构父/子/孙?
【发布时间】:2017-05-15 13:13:10
【问题描述】:

像父/子层次结构一样对“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'),]

【问题讨论】:

  • 期望的输出是什么?深度优先?广度优先?完全不同的东西?查看 Python 文档中的 Sorting How To
  • a = [('100', '工作室', '000'), ('101', '工作室', '000'), ('111', '工作室', '000 '), ('555', '工作室', '111'), ('222', '工作室', '111'), ('333', '设置部分', '222'), ('444' , '比例', '222'), ('110', '工作室', '101'), ('120', '工作室', '000'), ('130', '工作室', '120' ), ('666', 'Workroom', '000'), ('777', 'Workroom', '666'), ('888', 'Setup Part', '777'),]
  • 是否应该 ('110', 'Workroom', '101'), 不直接跟在 ('101', 'Workroom', '000'), 之后并在您的可视化中缩进一级?
  • 看看:stackoverflow.com/questions/34964878/…。也许是重复的?
  • 是的,先生,对不起。

标签: python html


【解决方案1】:

根据以下正确答案:

Python - Generate a dictionary(tree) from a list of tuples

你可以试试:

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)

在答案中,我们为所有节点创建一个字典,然后我们为所有比添加一个子值,包含子节点。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多