I have an expression Tree, that generates the Tree list. The list variers depending on the expression that are used to calculate the number for example,
T = ['-', ['+', ['-', 75, ['-', 10, 3]], ['-', 100, 50]], 3]
Or
T = ['+', ['*', 5, 4] ,['-', 100, ['/', 20, 2] ]]
I want to iterate over each and every element of the Tree and store the index of the operators, list of all the operators, index of the numbers and the list of numbers in an array. For this, I am trying to figure out a way that I can loop into the list and then just check if the type is a string which would mean it is an operator and store that index and value in the respective array by appending, else if the type is number then store it in the array with all the numbers.
I tried the below code
T = T = ['-', ['+', ['-', 75, ['-', 10, 3]], ['-', 100, 50]], 3]
for i in range(0,len(T)):
if type(T[i]) != int:
for j in range(0,len(T[i])):
print(T[i][j])
else:
print(T[i])
Running the code gives the output:
-
+
['-', 75, ['-', 10, 3]]
['-', 100, 50]
3
What we are basically trying to achieve is to go inside the list till we could depending on the list size. Any ideas on how this could be possible?
My answer would basically be:
address list of the operators is [[0], [1, 0], [1, 1, 0], [1, 1, 2, 0], [1, 2, 0]]
list of the operators is ['-', '+', '-', '-', '-']
address of the numbers is [[1, 1, 1], [1, 1, 2, 1], [1, 1, 2, 2], [1, 2, 1], [1, 2, 2], [2]]
list of the numbers is [75, 10, 3, 100, 50, 3]