working in python and I am pretty new to loops. I need to loop through a 2D array with the first position being an ID number, and the second position being its parents ID number. The array looks something like this [2. 14][7. 2436][462. 324]
And so on. I need a loop that goes through the entire array, looking at the first column, identifying the parent, then finding where that parent ID exists in the first column, finding the new parent...etc etc etc. Until it finds an ID with a parent value of 2, where it places the very first ID it started with into a separate file/record.
I have some scrap code of different things I have tried, but am stuck on getting something that can loop through the array by element. Here is my current system
#Create array, will upload matrix file in true run
array = [[1, 2],
[98,100],
[2, 5],
[100,150],
[5, 6],
[12,69],
[6, 20],
[20, 40],
[69,420],
[40, 60]]
#name target ending value ID
superparent = 2
i=0
#outer loop, include counter to break endless loops
for x in array:
y=0
counter = 0
j = x
#inner loop, assign variables to each array element
for z in array:
if array[i][1] == array[y][0]:
m = z
k = i
while j[0]!=superparent:
j = array[k]
k = k-1
print(m)
print(j)
print("next")
y = y+1
i = i+1
thanks for any and all help!
