May-09-2021, 07:49 AM
(This post was last modified: May-09-2021, 07:53 AM by Gribouillis.)
This is a version of the problem of finding the connected components of a graph. One approach would be to use a library that already has a function to do that, such as networkx.
>>> import networkx as nx >>> g = nx.Graph() >>> ipath = [['a','b'],['c','d'],['b','e'],['f','g'],['a','h'],['i','c']] >>> for p in ipath: ... g.add_edges_from(zip(p, p[1:])) ... >>> for c in nx.connected_components(g): ... print(c) ... {'e', 'h', 'a', 'b'} {'i', 'c', 'd'} {'g', 'f'}