I'm having some trouble finding a way to get a list index against a list of nested lists.
For example I can find out how many nodes, or the structure of the list for a given node with the following two functions.
t = ['add', [ \
['divide a', [ \
['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']], \
['subtract', [ \
['add', [ \
['round to integer', ['Var 10']], 'Var 4'] \
], 'Var 9' \
]] \
]], 'Var 4' \
]]
def total_nodes(structure,num_nodes):
s = structure
print "N:" , num_nodes , s
num_nodes += 1
if isinstance(s,str): return num_nodes
if s[1]:
for x in range(len(s[1])):
num_nodes = total_nodes(s[1][x], num_nodes)
return num_nodes
def get_structure_for_node(structure,counter,find_node=1):
s = structure
if not isinstance(counter,int):
return counter
if counter == find_node:
return s
counter += 1
if isinstance(s,str): return counter
if s[1]:
for x in range(len(s[1])):
counter = get_structure_for_node(s[1][x],counter,find_node=find_node)
return counter
print
print total_nodes(t,0)
print
print get_structure_for_node(t,0,find_node=12)
OUTPUT:
N: 0 ['add', [['divide a', [['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']], ['subtract', [['add', [['round to integer', ['Var 10']], 'Var 4']], 'Var 9']]]], 'Var 4']]
N: 1 ['divide a', [['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']], ['subtract', [['add', [['round to integer', ['Var 10']], 'Var 4']], 'Var 9']]]]
N: 2 ['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']]
N: 3 Var 9
N: 4 Var 5
N: 5 Var 1
N: 6 Var 4
N: 7 ['subtract', [['add', [['round to integer', ['Var 10']], 'Var 4']], 'Var 9']]
N: 8 ['add', [['round to integer', ['Var 10']], 'Var 4']]
N: 9 ['round to integer', ['Var 10']]
N: 10 Var 10
N: 11 Var 4
N: 12 Var 9
N: 13 Var 4
14
Var 9
From the output I can see the path from t to the node '12' we searched for would be:
t[1][0][1][1][1][1]
but I have been unable to find a way to keep track of this index key as I am going through the recursive function. which I require to change elements of the list / tree
Any takers?
I should add that I had been trying to track it by adding in a variable to the recursion which builds a string of where it went i.e.
path = "10112101"
then trying to work with this later, however I've been unable to get it accurate and would prefer a cleaner way.