here is the problem: I have a list of tuples (could be sets as well if needed). For instance:
a = [(1, 5), (4, 2), (4, 3), (5, 4), (6, 3), (7, 6)]
What I want to find is a list
r = [(1, 5, 4, 2, 3, 6, 7)]
because the intersection is not empty once all the sets are put together.
For the example
a = [(1, 5), (4, 2), (4, 3), (5, 4), (6, 3), (7, 6), (8, 9)]
the result should be
r = [(1, 5, 4, 2, 3, 6, 7), (8, 9)]
Hope the problem is clear. So what is the most elegant way to do this in python, if any?
Cheers
These are the connected components of a graph, and can be found using a graphing library such as
networkx
. For your second example: