newbe how to read in a string?

Chris Gonnerman chris.gonnerman at usa.net
Fri Mar 23 14:01:32 EST 2001


----- Original Message ----- 
From: "Alex Martelli" <aleaxit at yahoo.com>
Subject: Re: newbe how to read in a string?


> "Chris Gonnerman" <chris.gonnerman at usa.net> wrote in message
> news:mailman.985355670.6600.python-list at python.org...
>     [snip]
> >     import string
> >     seq = string.split(a, ",")
> >     for i in range(1, len(seq)): # this skips the 0 index,
> >         seq[i] = float(seq)      # which needs to stay a string.
> >
> > In Python 2.0 the code might be shorter, using string methods and list
> > comprehension, but I personally prefer the "old way" myself.
> 
> The 'old way' would be
>     seq = map(float, string.split(a, ','))
> and it still works fine of course, though I'd rather spell it
>     seq = map(float, ','.split(a))

BZZT!  The given string starts with a date which will cause float()
to throw a ValueError.

I might say:

    seq = string.split(a, ",")
    seq[2:] = map(float, seq[2:])

> List comprehensions are great, but when you DO want to get
> map's primary job (applying an already-existing function
> to parameter sequences) map is still best (with list
> comprehension, you can avoid using _lambda_ just in order
> to be able to use map -- I think eschewing lambda makes
> your code clearer, but maybe that's just me).
 
No argument here.  I work in both 1.5.2 and 2.0 and rarely miss
the 2.0 features, and I'm in the camp that finds ','.split(a) 
ugly.  I don't like lambda and avoid it whenever possible.
 






More information about the Python-list mailing list