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]
>Solution :
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