Reputation: 65
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!
Upvotes: 2
Views: 283
Reputation: 4368
I did not understood the question. I could not explain the reasons in just one comment, so this is an answer to "why is the problem unclear ?".
Given your data :
array = [[1, 2],
[98,100],
[2, 5],
[100,150],
[5, 6],
[12,69],
[6, 20],
[20, 40],
[69,420],
[40, 60]]
I created the Graphviz file :
digraph G {
1 -> 2;
98 -> 100;
2 -> 5;
100 -> 150;
5 -> 6;
12 -> 69;
6 -> 20;
20 -> 40;
69 -> 420;
40 -> 60;
2 [shape=Msquare];
}
What is the expected output in this case ? Is it a problem that there are 3 "components" (in graph theory terminology) ? Why is the superparent "2" child of "5" (based on your definition that each pair is <child_id, parent_id> ) ? Following your algorithm ("Until it finds an ID with a parent value of 2"), it means I should stop after the very first iteration ([1,2]
being the only couple where parent_id is 2) ?
Lacking answers to these question, this is the best I could think of :
from typing import Optional, Dict, List, Sequence
array = [[1, 2],
[98,100],
[2, 5],
[100,150],
[5, 6],
[12,69],
[6, 20],
[20, 40],
[69,420],
[40, 60]]
def search_parent_of(node_id: int) -> Optional[int]:
# search a line matching the child, and return the parent or None
return next((parent_id for child_id, parent_id in array if child_id == node_id), None)
def compute_ancestors() -> Dict[int, Sequence[int]]:
ancestors: Dict[int, List[int]] = {}
for child_id, parent_id in array:
ancestors[child_id] = [parent_id]
while ancestor_id := search_parent_of(parent_id):
ancestors[child_id].append(ancestor_id)
parent_id = ancestor_id
return ancestors
for child, ancestors in sorted(compute_ancestors().items()):
print(child, ":", ancestors)
which gives :
1 : [2, 5, 6, 20, 40, 60]
98 : [100, 150]
2 : [5, 6, 20, 40, 60]
100 : [150]
5 : [6, 20, 40, 60]
12 : [69, 420]
6 : [20, 40, 60]
20 : [40, 60]
69 : [420]
40 : [60]
which corresponds to the visualization.
Please update your question and notify me after.
Upvotes: 2