我目前正在尝试从包含某些脚本的数据框中创建一个树形图,这些脚本需要根据它们的parent/child关系按特定顺序执行。但是,我没有收到正确数量的孩子。我的 DataFrame 如下所示:
这是我到目前为止的代码:
from anytree import Node, RenderTree
def add_nodes(nodes, parent, child):
if parent not in nodes:
nodes[parent] = Node(parent)
if child not in nodes:
nodes[child] = Node(child)
nodes[child].parent = nodes[parent]
nodes = {}
for parent, child in zip(df["parent_script"],df["child_script"]):
add_nodes(nodes, parent, child)
roots = list(df[~df["parent_script"].isin(df["child_script"])]["parent_script"].unique())
for root in roots:
for pre, _, node in RenderTree(nodes[root]):
print("%s%s" % (pre, node.name))
我有 3 个根和几个正确显示的父母,但孩子们在被包含一次后似乎失踪了。
我得到的结果:
我认为由于这种逻辑,我没有收到我期望的全部结果。
if child not in nodes:
nodes[child] = Node(child)
nodes[child].parent = nodes[parent]
我需要所有的孩子都在树里面。(00_06_MaxBIS_v2-2.sql)因此,例如在 3rd Root00_07_AnlagenDatenLaden_v1-7.sql小时候,但缺少00_Gebietsstrukturen_v1-2.sql.
有没有办法包含重复的孩子并将它们正确地放在树内?我可以使用什么样的逻辑来接收预期的结果?

