I have a list of tuples like this
l = [('hin1','ie2',2),('hin1','ie3',2),('hin4','ie5',2),('hin6','ie22',2),('hin1','ie32',2),('hin31','ie2',2),('hin61','ie62',2)]
I want to merge the tuples which share atleast one common element between the two
Hence - two tuples like this :
('hin1','ie2',2),('hin1','ie3',2) should result in
(('hin1','ie2', 'ie3')
For the above list l, my final output should be like this
output - [(hin1,ie2,ie3,ie32,hin31),(hin4,ie5),(hin6,ie22),(hin61,ie62)]
Note - The 3rd element of every tuple can be ignored
Any starting points?
This is a network analysis problem. You can use
igraphpackage if you do not intend to write your own algorithm to solve it: