Can a List Comprehension do ____ ?

Eric @ Zomething eric at zomething.com
Wed Jul 14 03:22:08 EDT 2004


Pythonistas,

I seem at a loss for a List Comprehension syntax that will do what I want.

I have a list of string position spans:

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


Of course I can get such a list, but I haven't been able to figure out how to do this with a List Comprehension.  I am wondering if there is a syntax which makes pairs of the maximum of one list element and the minimum of the next list element.

Can anyone show me the List Comprehensions light?

Was trying things like this:  

>>> listSpans=[(max(x,y),min(a,b)) for (x,y) in breaks for (a,b) in breaks.pop()]




With regards



Eric Pederson
http://www.songzilla.blogspot.com
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
e-mail me at:
do at something.com
except, increment the "d" and "o" by one letter
and spell something with a "z"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



More information about the Python-list mailing list