Merge pairs on common integer with restrictions
Merge pairs on common integer with restrictions I need an efficient way to merge a list of nodes (pairs of integers). The merge should happen only if there is one common number in the pair and the common number is on the first or the last position (otherwise its allready connected). For example: mylist = [[4, 3], [6, 3]] merge_links(mylist) # should output [4, 3, 6] another example: mylist = [[4, 3], [6, 3], [6, 4]] merge_links(mylist) # should output again [4, 3, 6] because both 6 and 4 allready exist in array. and yet another example: mylist = [[4, 3], [6, 3], [6, 4], [6, 2], [7, 4], [4, 9]] merge_links(mylist) # should output [7, 4, 3, 6, 2] # [4, 3] ✔ # [4, 3] + [6, 3] ✔ -> [4, 3, 6] # [4, 3, 6] + [6, 3] ✘ both 6 and 3 exist in [4, 3, 6] # [4, 3, 6] + [6, 4] ✘ both 6 and 4 exist in [4, 3, 6] # [4, 3, 6] + [6, 2] ✔ -> [4, 3, 6, 2] # [4, 3, 6, 2] + [7, 4] ✔ -> [7, 4, 3, 6, 2] # [7, 4, 3, 6, 2] + [4, 9] ✘ 4 is allready connected "7-4-3"! Currently I'm u