0

I have a list of tuples which represent timeslots (start_epoch, end_epoch). What I'm trying to accomplish is the following:

If have this list:

[
    (1398932511, 1398932991),
    (1398933052, 1398933142),
    (1400018571, 1400020941),
    (1400020941, 1400024572),
    (1400024572, 1400027541),
    (1400027602, 1400028172),
    (1400824012, 1400824162)
]

I want the tuples which are consecutive to be joined together in a new list. So the result would be this:

[
    (1398932511, 1398932991),
    (1398933052, 1398933142),
    (1400018571, 1400027541),
    (1400027602, 1400028172),
    (1400824012, 1400824162)
]

I have been looking around (stackoverflow, Google) but I'm unable to find a proper solution to this. I can't figure out how to properly use itertools for python (I'm fairly new to Python :) ).

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Fabian76
  • 389
  • 2
  • 11
  • 1
    I can't see the difference between the 1st and the 2nd list. – Michał Szydłowski Jun 02 '14 at 14:13
  • @MichałSzydłowski: the elements `(1400018571, 1400020941), (1400020941, 1400024572), (1400024572, 1400027541)` have been merged into `(1400018571, 1400027541)`. – Martijn Pieters Jun 02 '14 at 14:18
  • Are your ranges always sorted? Or could they be out of order? – Martijn Pieters Jun 02 '14 at 14:18
  • The ranges are always sorted – Fabian76 Jun 02 '14 at 14:19
  • Okay, now that the lists are better formatted, it is clear. – Michał Szydłowski Jun 02 '14 at 14:23
  • @MartijnPieters kind of disagree with the dupe, unfortunately. His input is already sorted, his interval boundaries match exactly, and (probably most importantly) none of the answers to that question are particularly good. – roippi Jun 02 '14 at 14:34
  • @roippi: It doesn't matter here that the boundaries match or overlap, the accepted answer works for either case. If you feel you can write a better answer, then do so for that question; its still a dupe. – Martijn Pieters Jun 02 '14 at 14:36
  • On reflection, that's probably the correct thing to do. – roippi Jun 02 '14 at 14:39
  • Thanks for all the replies. I didn't find the duplicate. Probably used the wrong search. – Fabian76 Jun 03 '14 at 08:51
  • 1 more thing. When I posted the question, i saw that the indentation for the code block didn't work (I did use 4 spaces, but it didn't work). Before I could figure out what went wrong, somebody allready edited my question (I believe it was @MartijnPieters). How is a newby like myself going to learn how to post questions properly? – Fabian76 Jun 03 '14 at 08:56
  • I only meant to help there, @Fabian. You can use the [formatting sandbox](http://meta.stackexchange.com/questions/3122/formatting-sandbox) if you wanted to practice your post formatting skills, but I find the preview to work just fine. Hopefully my formatting changes can help you learn? Badly formatted questions can hurt how they are received which is why experienced users edit new posts into shape. – Martijn Pieters Jun 03 '14 at 12:37
  • I know and thank you all for the help. I didn't know about a formatting sandbox. I will be practicing there. Tnx! – Fabian76 Jun 04 '14 at 14:29

0 Answers0