import and package confusion

Arnaud Delobelle arnodel at
Fri May 1 08:24:42 CEST 2009

Dale Amon <amon at> writes:

> Now I can move on to parsing those pesky Fortran card
> images... There wouldn't happen to be a way to take n
> continguous slices from a string (card image) where each 
> slice may be a different length would there? Fortran you 
> know. No spaces between input fields. :-)
> I know a way to do it, iterating over a list of slice sizes,
> perhaps in a list comprehension, but some of the august python 
> personages here no doubt know better ways.

It's a challenge to do it in a list comprehension, but here it is!

Warning: unpythonic code follows(*).  Sensitive readers may want to look
away now.

>>> data
>>> field_sizes
[3, 5, 9, 2]
>>> [data[i:j] for j in [0] for s in field_sizes for i, j in [(j, j+s)]]

Now you can look again.

(*) Pythonicity is often much debated as it lies for a good part in the
eye of the snake charmer.  I don't think this snippet would generate
much debate!


More information about the Python-list mailing list