Find linked elements betwenn two list
Question:
Probably this is a very stupid mistake but I can’t find a solution. I have the lists below:
node_pair = [('a', 'b'), ('b', 'a')]
edge_list = [
(1, 'unuseful thing', {'orig-id': 'a'}),
(11, 'unuseful thing', {'orig-id': 'b'}),
]
I need to find the numeric couple in edge_list
that is related to node_pair
. For ('a', 'b')
I will see (1, 11)
and for ('b', 'a')
I will see (11, 1)
. Below my code:
for pair in node_pair:
start_node = pair[0][0]
end_node = pair[1][0]
print(f'from {start_node} --> to {end_node}')
graph_start_end_node = []
for edge in edge_list:
edge_orig_id = edge[2]['orig-id']
if start_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node.append(edge_start_node)
if end_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node.append(edge_start_node)
print(graph_start_end_node)
The result is:
from a --> to b
[1, 11]
from b --> to a
[1, 11]
Answers:
Using append
works only when the order in the node_pair
list is the same as in the edge_list
, because this way the value is added at the end of the list. When the order could vary you have to place the matched value in a specific position – like:
graph_start_end_node = [None, None]
for edge in edge_list:
edge_orig_id = edge[2]['orig-id']
if start_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node[0] = edge_start_node
if end_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node[1] = edge_start_node
I found it hard to fix your code so I tried it this way.
node_pair = [('a', 'b'), ('b', 'a')]
edge_list = [
(1, 'unuseful thing', {'orig-id': 'a'}),
(11, 'unuseful thing', {'orig-id': 'b'}),
]
for pair in node_pair:
start_node, end_node = pair
print(f'from {start_node} --> to {end_node}')
graph_start_end_node = [e[0] for p in pair for e in edge_list if e[2]['orig-id'] == p ]
print(graph_start_end_node)
Make a new list with a list iteration is generally easier than trying to append to an existing one.
Probably this is a very stupid mistake but I can’t find a solution. I have the lists below:
node_pair = [('a', 'b'), ('b', 'a')]
edge_list = [
(1, 'unuseful thing', {'orig-id': 'a'}),
(11, 'unuseful thing', {'orig-id': 'b'}),
]
I need to find the numeric couple in edge_list
that is related to node_pair
. For ('a', 'b')
I will see (1, 11)
and for ('b', 'a')
I will see (11, 1)
. Below my code:
for pair in node_pair:
start_node = pair[0][0]
end_node = pair[1][0]
print(f'from {start_node} --> to {end_node}')
graph_start_end_node = []
for edge in edge_list:
edge_orig_id = edge[2]['orig-id']
if start_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node.append(edge_start_node)
if end_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node.append(edge_start_node)
print(graph_start_end_node)
The result is:
from a --> to b
[1, 11]
from b --> to a
[1, 11]
Using append
works only when the order in the node_pair
list is the same as in the edge_list
, because this way the value is added at the end of the list. When the order could vary you have to place the matched value in a specific position – like:
graph_start_end_node = [None, None]
for edge in edge_list:
edge_orig_id = edge[2]['orig-id']
if start_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node[0] = edge_start_node
if end_node == edge_orig_id:
edge_start_node = edge[0]
graph_start_end_node[1] = edge_start_node
I found it hard to fix your code so I tried it this way.
node_pair = [('a', 'b'), ('b', 'a')]
edge_list = [
(1, 'unuseful thing', {'orig-id': 'a'}),
(11, 'unuseful thing', {'orig-id': 'b'}),
]
for pair in node_pair:
start_node, end_node = pair
print(f'from {start_node} --> to {end_node}')
graph_start_end_node = [e[0] for p in pair for e in edge_list if e[2]['orig-id'] == p ]
print(graph_start_end_node)
Make a new list with a list iteration is generally easier than trying to append to an existing one.