Python Forum
List of lists - merge sublists with common elements - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: List of lists - merge sublists with common elements (/thread-33591.html)



List of lists - merge sublists with common elements - medatib531 - May-09-2021

Assume I have list1 as follows:

list1 = [['a','b'],['c','d'],['b','e'],['f','g'],['a','h'],['i','c']]
I want to merge the sublists that have common elements, so based on the above example the resulting list will be

list2 = [['a','b','e','h'],['c','d','i'],['f','g']]
I could do things like parse each element separately and do comparisons all over etc. but the input list can be pretty long (maybe many hundreds or even millions of elements overall) Is there an efficient way to do this in python?


RE: List of lists - merge sublists with common elements - Gribouillis - May-09-2021

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'}