I need to combine overlapping ranges of numbers into single range. So I have a list with sub-lists of something like:
[[83,77],[103,97],[82,76],[101,95],[78,72],[97,91],[72,66],[89,83],[63,57],[78,72],[53,47],[65,59],[41,35],[50,44],[28,22],[34,28],[14,8],[16,10]]
So from 83 to 77 is overlapping 82 to 76 and will become 76 to 83. If any other ranges overlap this range would ad it's minimum of maximum to this range, and when there no others that overlap then the method should go to the next one in the list and try to merge that with it's overlappings.
I hoop this makes sense.
Use an IntervalTree https://en.wikipedia.org/wiki/Interval_tree
There is an implementation available in python:
pip install intervaltree
import intervaltree
intervals = [
[77, 83],
[97, 103],
[76, 82],
[95, 101],
[72, 78],
[91, 97],
[66, 72],
[83, 89],
[57, 63],
[72, 78],
[47, 53],
[59, 65],
[35, 41],
[44, 50],
[22, 28],
[28, 34],
[8, 14],
[10, 16],
]
tree = intervaltree.IntervalTree.from_tuples(intervals)
print(tree)
tree.merge_overlaps()
print(tree)
tree.merge_overlaps(strict=False)
print(tree)
note that I had to make your points be (start, end)
instead of (end, start)
.
IntervalTree([Interval(8, 14), Interval(10, 16), Interval(22, 28), Interval(28, 34), Interval(35, 41), Interval(44, 50), Interval(47, 53), Interval(57, 63), Interval(59, 65), Interval(66, 72), Interval(72, 78), Interval(76, 82), Interval(77, 83), Interval(83, 89), Interval(91, 97), Interval(95, 101), Interval(97, 103)])
is merged to
IntervalTree([Interval(8, 16), Interval(22, 28), Interval(28, 34), Interval(35, 41), Interval(44, 53), Interval(57, 65), Interval(66, 72), Interval(72, 83), Interval(83, 89), Interval(91, 103)])
and with strict=False
allowing touching intervals to be merged
IntervalTree([Interval(8, 16), Interval(22, 34), Interval(35, 41), Interval(44, 53), Interval(57, 65), Interval(66, 89), Interval(91, 103)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With