I have the table below that I read into a dataFrame:
n,next_n
1,2
1,3
1,6
2,4
2,8
3,5
3,9
4,7
9,10
My recursive function should return multiple lists of numbers through the end.
For example if I select to see all the values associated with 9, I should get a list that reads [9,10].
Another example:
4 should yield [4,7]
3 yields two lists
[3,5]
[3,9,10]
def recursivenum(df,n): indices = list() indices.append(n) x = df[df['n'] == n].next_n if len(x) > 0: for p1 in df[df['n'] == n].next_n: indices = indices + recursivenum(df,p1) elif len(x) == 0: #Base case - There are no other values print(indices) return indices
When I run recursivenum(df,1)
I get
[7]
[8]
[5]
[10]
[6]
[1, 2, 4, 7, 8, 3, 5, 9, 10, 6]
Which is nothing compared to what I expect to see.
I expect to see five lists: [1,2,4,7]
[1,2,8]
[1,3,5]
[1,3,9,10]
[1,6]
Can someone point me in the right direction?