import and package confusion
arnodel at googlemail.com
Fri May 1 08:24:42 CEST 2009
Dale Amon <amon at vnl.com> 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
[3, 5, 9, 2]
>>> [data[i:j] for j in  for s in field_sizes for i, j in [(j, j+s)]]
['AAA', 'BBBBB', 'CCCCCCCCC', 'DD']
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
More information about the Python-list