List of strings to list of floats ?

Alex Martelli aleaxit at yahoo.com
Tue Oct 18 14:40:54 EDT 2005


Madhusudan Singh <spammers-go-here at spam.invalid> wrote:
   ...
> >> Say I have two lists of floats. And I wish to generate a list of floats
> >> that is a user defined function of the two lists.
> > 
> > result = [sqrt(x**2 + y**2) for x, y in zip(xs, ys)]
> 
> Works perfectly. Thanks !

If zip works and map doesn't, most likely your problem is that the two
lists have different lengths.  In this case, zip truncates to the
shorter list, while map conceptually extends the shorter list with
copies of None -- causing an error when you try to do arithmetic between
a float towards the end of the longer list, and None...!


Alex



More information about the Python-list mailing list