Can a List Comprehension do ____ ?

Peter Otten __peter__ at web.de
Wed Jul 14 04:12:08 EDT 2004


Eric @ Zomething wrote:

>>>> breaks
> [(133, 137), (181, 185), (227, 231), (232, 236), (278, 282), (283, 287),
> [(352, 356), (412, 416), (485, 489), (490, 494)]
> 
> the first pair representing: someString[133:137]
> 
> What I want is a list of the positions between these spans, which would
> look like this:
> 
>>>> spans
> [[(137, 181)], [(185, 227)], [(231, 232)], [(236, 278)], [(282, 283)],
> [[[(287, 352)], [(356, 412)], [(416, 485)], [(489, 490)]]
> 

>>> 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)]
>>>

Peter




More information about the Python-list mailing list