473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

List of strings to list of floats ?

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.
Oct 16 '05 #1
8 4852

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
Oct 16 '05 #2
sk**@pobox.com wrote:

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.
Oct 17 '05 #3
Madhusudan Singh wrote:
sk**@pobox.com wrote:

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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 17 '05 #4
Steve Holden wrote:
Madhusudan Singh wrote:
sk**@pobox.com wrote:

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 ?
Oct 17 '05 #5
Madhusudan Singh wrote:
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)]

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Did you ever love somebody / Did you ever really care
-- Cassandra Wilson
Oct 17 '05 #6
Erik Max Francis wrote:
Madhusudan Singh wrote:
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 !
Oct 17 '05 #7
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

Oct 17 '05 #8
Madhusudan Singh <sp**************@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
Oct 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
50
by: dataangel | last post by:
I wrote a function to compare whether two strings are "similar" because I'm using python to make a small text adventure engine and I want to it to be responsive to slight mispellings, like...
0
by: andrea_gavana | last post by:
Hello NG, probably because I still have Python 2.3.4, these are the results I'm getting: C:\Python23\Lib>python timeit.py -s "floats = map(float, range(1000))" "ints = m ap(int, floats)"...
12
by: BGP | last post by:
I am working on a WIN32 API app using devc++4992 that will accept Dow Jones/NASDAQ/etc. stock prices as input, parse them, and do things with it. The user can just cut and paste back prices into a...
9
by: jm.suresh | last post by:
Hi, I have a string '((1,2), (3,4))' and I want to convert this into a python tuple of numbers. But I do not want to use eval() because I do not want to execute any code in that string and limit...
11
by: Steve | last post by:
I'm trying to create a list range of floats and running into problems. I've been trying something like: a = 0.0 b = 10.0 flts = range(a, b) fltlst.append(flts)
10
by: rshepard | last post by:
While working with lists of tuples is probably very common, none of my five Python books or a Google search tell me how to refer to specific items in each tuple. I find references to sorting a list...
16
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
6
by: [david] | last post by:
returns poorly formatted values: '13.3' ''
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.