【发布时间】:2023-04-10 12:26:01
【问题描述】:
给定一个父节点,我想获取它的 N 级节点(原始数据框要大得多)
child| level| parent|
40| 1| 40|
21| 2| 40|
18| 2| 40|
37| 3| 21|
2| 3| 18|
85| 3| 21|
14| 4| 37|
58| 4| 2|
47| 4| 37|
34| 4| 85|
45| 4| 18|
32| 4| 2|
47| 4| 85|
88| 4| 85|
12| 4| 37|
我做什么:
def get_children(x, tree, lst):
for nod in tree[tree['parent'] == x]['child']:
get_children(nod, tree, lst)
lst.append(nod)
然后我过滤所有N级节点。
我想要另一种方式,因为当数据框较大时,递归调用过多。
【问题讨论】:
标签: python-3.x pandas algorithm recursion