I am trying to code a fast function which will loop through the elements in the sublists and merge the sublists if they contain element. For example, the list [[0, 3], [3, 4], [5, 6]]
should be merged to [[0, 3, 4], [5, 6]].
The sublists can be of any size and each sublist can have a different size, therefore could contain many elements.
My code so far (which does not work) is shown below. The error that comes up is: slice indices must be integers or None or have an __index__ method
def join_clusters(clusters):
for cluster in clusters:
for j in cluster:
for k in clusters[cluster:]:
for h in k:
if j == h:
cluster.append(k)
clusters.pop(k)
return clusters