List of strings to list of floats ?

M

Madhusudan Singh

Is it possible to convert a very long list of strings to a list of floats in
a single statement ?

I have tried float(x) and float(x[:]) but neither work. I guess I would have
to write a loop if there isn't a way.
 
S

skip

Madhusudan> Is it possible to convert a very long list of strings to a
Madhusudan> list of floats in a single statement ?

Madhusudan> I have tried float(x) and float(x[:]) but neither work. I
Madhusudan> guess I would have to write a loop if there isn't a way.

Try:
>>> los = ["123.0", "2", "1e-6"]
>>> map(float, los) [123.0, 2.0, 9.9999999999999995e-07]
>>> [float(s) for s in los]
[123.0, 2.0, 9.9999999999999995e-07]

Skip
 
M

Madhusudan Singh

Madhusudan> Is it possible to convert a very long list of strings to a
Madhusudan> list of floats in a single statement ?

Madhusudan> I have tried float(x) and float(x[:]) but neither work. I
Madhusudan> guess I would have to write a loop if there isn't a way.

Try:
los = ["123.0", "2", "1e-6"]
map(float, los) [123.0, 2.0, 9.9999999999999995e-07]
[float(s) for s in los]
[123.0, 2.0, 9.9999999999999995e-07]

Skip

Thanks. Now, a slightly more complicated question.

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.

I tried :

def rpyth(x,y):
r=sqrt(pow(x,2.0)+pow(y,2.0))
return r

r1n=map(rpyth,x2n[1:len(x2n)-1],y2n[1:len(y2n)-1])

And I get an error complaining about floats.
 
S

Steve Holden

Madhusudan said:
Madhusudan> Is it possible to convert a very long list of strings to a
Madhusudan> list of floats in a single statement ?

Madhusudan> I have tried float(x) and float(x[:]) but neither work. I
Madhusudan> guess I would have to write a loop if there isn't a way.

Try:
los = ["123.0", "2", "1e-6"]
map(float, los)
[123.0, 2.0, 9.9999999999999995e-07]
[float(s) for s in los]
[123.0, 2.0, 9.9999999999999995e-07]

Skip


Thanks. Now, a slightly more complicated question.

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.

I tried :

def rpyth(x,y):
r=sqrt(pow(x,2.0)+pow(y,2.0))
return r

r1n=map(rpyth,x2n[1:len(x2n)-1],y2n[1:len(y2n)-1])

And I get an error complaining about floats.

.... but the error message is a secret so you don't want to tell us what
it was? It's more helpful if you actually copied and pasted the exact
error traceback you see, as this avoids any potential answerer having to
guess what went wrong.

A brief description of what you were trying to do is also helpful, since
it isn't obvious at first glance why you are omitting some of hte list
elements.

regards
Steve
 
M

Madhusudan Singh

Steve said:
Madhusudan said:
Madhusudan> Is it possible to convert a very long list of strings to
a Madhusudan> list of floats in a single statement ?

Madhusudan> I have tried float(x) and float(x[:]) but neither work. I
Madhusudan> guess I would have to write a loop if there isn't a way.

Try:

los = ["123.0", "2", "1e-6"]
map(float, los)
[123.0, 2.0, 9.9999999999999995e-07]
[float(s) for s in los]
[123.0, 2.0, 9.9999999999999995e-07]

Skip


Thanks. Now, a slightly more complicated question.

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.

I tried :

def rpyth(x,y):
r=sqrt(pow(x,2.0)+pow(y,2.0))
return r

r1n=map(rpyth,x2n[1:len(x2n)-1],y2n[1:len(y2n)-1])

And I get an error complaining about floats.

... but the error message is a secret so you don't want to tell us what
it was? It's more helpful if you actually copied and pasted the exact
error traceback you see, as this avoids any potential answerer having to
guess what went wrong.

A brief description of what you were trying to do is also helpful, since
it isn't obvious at first glance why you are omitting some of hte list
elements.

regards
Steve

I apologize for that.

The error message is :

Traceback (most recent call last):
File "takedata.py", line 163, in ?
r1n=map(rpyth,x2n[1:len(x2)-1],y2n[1:len(y2)-1])
File "takedata.py", line 15, in rpyth
r=sqrt(pow(x,2.0)+pow(y,2.0))
TypeError: a float is required

I understand why the error is occuring (pow is not overloaded to accept
lists of floats instead of floats ?). How do I fix this ?
 
E

Erik Max Francis

Madhusudan said:
Thanks. Now, a slightly more complicated question.

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

Madhusudan Singh

Erik said:
Madhusudan said:
Thanks. Now, a slightly more complicated question.

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 !
 
B

bearophileHUGS

Erik Max Francis:
result = [sqrt(x**2 + y**2) for x, y in zip(xs, ys)]

Another possibility:

from math import hypot
result = [hypot(*xy) for xy in zip(xs, ys)]

Or better:
from math import hypot
result = map(hypot, xs, ys)

bearophile
 
A

Alex Martelli

Madhusudan Singh said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top