I have a segments list:
lst = [[1,5],[2,7],[8,13],[12,15],[3,4]]
First and second elements intersect, so i combine them and so on.I need to get this list:
lst1 = [[1,7],[8,15]]
I have problem when 3 or more segments intersect with ourselves. I tried to fill the missing numbers, and find union using sets
unions = []
for i in range(len(lst)):
for j in range(i,len(lst)):
if len(list(set(lst[i]) & set(lst[j]))) != 0:
unions.append(list(set(lst[i]) | set(lst[j])))
but I think it is wrong way.How can I do this without using any libraries?