您可以使用treelib执行以下 *** 作:
from treelib import Node, Treedict_ = {"2": {'parent': "1"}, "1": {'parent': None}, "3": {'parent': "2"}}added = set()tree = Tree()while dict_: for key, value in dict_.items(): if value['parent'] in added: tree.create_node(key, key, parent=value['parent']) added.add(key) dict_.pop(key) break elif value['parent'] is None: tree.create_node(key, key) added.add(key) dict_.pop(key) breaktree.show()
输出量
1└── 2 └── 3
None。当父母被
None添加为根。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)