1

Consider the example:

a = [[95.958, 101.566], [117.665, 121.995], [138.909, 153.961],
     [154.529, 252.391], [229.633, 232.087], [268.995, 269.266],
     [269.432, 283.028], [286.432, 364.575], [366.179, 437.013],
     [446.27, 601.232], [585.713, 1374.37], [1287.678, 2464.882], 
     [2273.926, 5218.017]]

Based on the above array elements

[446.27, 601.232], [585.713, 1374.37], 
[1287.678, 2464.882], [2273.926, 5218.017]

These overlap with each-other. So to from an non-overlapping range these elements become a single element

[446.27, 5218.017]

so the final array will become

a = [[95.958, 101.566], [117.665, 121.995], [138.909, 153.961],
     [154.529, 252.391], [229.633, 232.087], [268.995, 269.266], 
     [269.432, 283.028], [286.432, 364.575], [366.179, 437.013],
     [446.27,5218.017]]

if anyone could suggest a code to do this in python it will be helpful?

Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
user3405360
  • 91
  • 1
  • 8
  • Is this a list of ranges? And you want to merge overlapping ranges? – vz0 Apr 25 '14 at 12:48
  • @vz0 each element of array i,e [95.958, 101.566] is a range from 95.958 sec to 101.566 sec.Yes i want to merge overlapping ranges – user3405360 Apr 25 '14 at 12:51
  • possible duplicate of [Merging a list of time-range tuples that have overlapping time-ranges](http://stackoverflow.com/questions/5679638/merging-a-list-of-time-range-tuples-that-have-overlapping-time-ranges) – Martijn Pieters Apr 25 '14 at 13:54

1 Answers1

0

Let your left hand and right hand reference one item at a time, and an output list for the merge result.

  1. Starting with the first element on the sequence, pick it with your left hand.
  2. Pick the next one in your right hand.
  3. Check if your left hand overlaps with your right hand:
    • If they overlap, merge your left hand with your right hand and keep it with your left hand
    • If no overlap, add your left hand item to the output list, and move your right hand to your left hand.
  4. Goto 2 until no more items remains.
  5. If your left hand has an item, add it to the output.
vz0
  • 32,345
  • 7
  • 44
  • 77