I need your help to figure out this problem.
when I use this code, I can get this result.
input=[[1],[1,2],[5,7]]
output=[[1,2],[5,7]]
Tb2 = Tb[:1]
for t in Tb[1:]:
if set(t).isdisjoint(Tb2[-1]):
Tb2.append(t)
else:
Tb2[-1] = sorted({*t,*Tb2[-1]})
But I can't solve the problem when another second list have same number in input.
input=[[2,3],[1,2],[5,7],[5,8],[7,8,9],[1]]
expected output=[[1,2,3],[5,7,8,9]]
Would you give me advice or help?
CodePudding user response:
This seems like connected components problem from graph theory, so you could use networkx for it:
import networkx as nx
from itertools import combinations
# input
lst = [[2, 3], [1, 2], [5, 7], [5, 8], [7, 8, 9], [1]]
# build graph
g = nx.Graph()
g.add_edges_from([edge for ls in lst for edge in combinations(ls, 2)])
# compute components
components = nx.algorithms.components.connected_components(g)
res = list(components)
print(res)
Output
[{1, 2, 3}, {8, 9, 5, 7}]
The idea is to build an edge between each pair of elements of the same list, this is achieved with the following list comprehension:
[edge for ls in lst for edge in combinations(ls, 2)]
# [(2, 3), (1, 2), (5, 7), (5, 8), (7, 8), (7, 9), (8, 9)]
Once that is done simply run the connected_components
algorithm on the graph.
CodePudding user response:
for t in Tb:
for j in range(len(Tb2)):
if set(t).isdisjoint(Tb2[j]):
Tb2.append(t)
else:
Tb2[j] = sorted({*t,*Tb2[j]})
Output= [[1, 2, 3], [5, 7, 8, 9], [5, 7, 8, 9], [7, 8, 9], [1], [1], [1]]
I can't eliminate same and small size of double lists.