Can a List Comprehension do ____ ?

Peter Otten __peter__ at web.de
Thu Jul 15 04:49:35 EDT 2004


Marc Boeren wrote:

>> >>> breaks = [(133, 137), (181, 185), (227, 231), (232, 236),
>> (278, 282),
>> (283, 287), (352, 356), (412, 416), (485, 489), (490, 494)]
>> >>> it = iter(breaks)
>> >>> [(start, end) for ((_, start), (end, _)) in zip(it, it)]
>> [(137, 181), (231, 232), (282, 283), (356, 412), (489, 490)]
>> >>>
> 
> That's not quite the result the OP wanted (it misses e.g. (185, 227)),

Silly me :(

Here's another one, using window() from the itertools examples:

>>> [(start, end) for ((_, start), (end, _)) in window(breaks)]
[(137, 181), (185, 227), (231, 232), (236, 278), (282, 283), (287, 352),
(356, 412), (416, 485), (489, 490)]

Peter




More information about the Python-list mailing list