Converting an array of string to array of float

J

joy99

Dear Group,

I got a question which might be possible but I am not getting how to
do it.

If I have a list, named,
list1=[1.0,2.3,4.4,5.5....]

Now each element in the array holds the string property if I want to
convert them to float, how would I do it?

Extracting the values with for and appending to a blank list it would
not solve the problem. If appended to a blank list, it would not
change the property.

If any one of the learned members can kindly suggest any solution?

Thanks in advance.
Best Regards,
Subhabrata.
 
B

Blockheads Oi Oi

I'm guessing you have something like

list1=['1.0', '2.3', '4.4', '5.5', ...], right?

You can do this:

for i in range(len(list1)):
list1 = float(list1)


or

for i, x in enumerate(list1):
list1 = float(x)
 
B

Blockheads Oi Oi

But you must be sure that the list only contains objects than can be
converted into float, if not you'll get:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
float('something')
ValueError: could not convert string to float: something

You learn something new every day :)
 
B

bruno.desthuilliers

Dear Group,

I got a question which might be possible but I am not getting how to
do it.

If I have a list, named,
list1=[1.0,2.3,4.4,5.5....]

Now each element in the array holds the string property if I want to
convert them to float, how would I do it?

Extracting the values with for and appending to a blank list it would
not solve the problem. If appended to a blank list, it would not
change the property.

If any one of the learned members can kindly suggest any solution?

print source ['0.0', '1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0', '8.0', '9.0']
source[:] = map(float, source)
print source [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

Note the "source[:] = " part - it modifies the list in place.
 
J

joy99

Dear Group,
I got a question which might be possible but I am not getting how to
do it.
If I have a list, named,
list1=[1.0,2.3,4.4,5.5....]
Now each element in the array holds the string property if I want to
convert them to float, how would I do it?
Extracting the values with for and appending to a blank list it would
not solve the problem. If appended to a blank list, it would not
change the property.
If any one of the learned members can kindly suggest any solution?

['0.0', '1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0', '8.0', '9.0']>>>source[:] = map(float, source)
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]



Note the "source[:] = " part - it modifies the list in place.

Thanks Bruno. I just missed it. I got it back and thanks Blockhead for
giving me new angle to look into the problem.
Best Regards,
Subhabrata.
 
S

Steven D'Aprano

Dear Group,

I got a question which might be possible but I am not getting how to do
it.

If I have a list, named,
list1=[1.0,2.3,4.4,5.5....]

That looks like a list of floats already. Perhaps you meant:

list1 = ["1.0", "2.3", "4.4", "5.5"]

Notice that the elements are now strings, not floats.

Now each element in the array holds the string property if I want to
convert them to float, how would I do it?

Extracting the values with for and appending to a blank list it would
not solve the problem. If appended to a blank list, it would not change
the property.

I don't understand what you mean. You want to get a list of floats. You
create a list of floats. How does that not solve the problem?

Wait, do you mean you want to change them *in-place*? Like this:

alist = ["1.1", "2.2", "3.3", "4.4", "5.5"]
blist = alist # reference to the same list object
alist[:] = map(float, alist)
print(blist[0] + 1) # prove that it is an in-place change
2.1


The key is the slice assignment: map(float, alist) creates a new list,
but the assignment alist[:] = ... stores those values in the original
list, instead of just binding the name to a new object. It is equivalent
to this:

temp = map(float, alist)
del alist[:] # empty the list in place
alist.extend(temp)
del temp # delete the variable

only easier and faster.


If you don't like map(), you can use a list comprehension:

alist[:] = [float(s) for s in alist]


Finally, if your list is so truly astonishingly huge that you don't have
room for it and the list-of-floats at the same time, hundreds of millions
of items or more, then you can change the list items in place:

for i, s in enumerate(alist):
alist = float(s)


But for small lists and merely large lists (millions or tens of millions)
of items, this will probably be much slower than the solutions shown
above. But your MMV -- time your code and find out for yourself.
 
A

Algis Kabaila

I'm guessing you have something like

list1=['1.0', '2.3', '4.4', '5.5', ...], right?

You can do this:

for i in range(len(list1)):
list1 = float(list1)


Better,

list1 = [float(v) for v in list1]

One statement only - long live list comprehension!

OldAl.
There's almost certainly a 1-liner you can use, but this
should work.

--Jason

Dear Group,

I got a question which might be possible but I am not
getting how to do it.

If I have a list, named,
list1=[1.0,2.3,4.4,5.5....]

Now each element in the array holds the string property if
I want to convert them to float, how would I do it?

Extracting the values with for and appending to a blank
list it would not solve the problem. If appended to a
blank list, it would not change the property.

If any one of the learned members can kindly suggest any
solution?

Thanks in advance.
Best Regards,
Subhabrata.
 
B

Blockheads Oi Oi

Thanks Bruno. I just missed it. I got it back and thanks Blockhead for
giving me new angle to look into the problem.
Best Regards,
Subhabrata.

Nothing personal, but it's Blockheads (plural) AND you missed the OI OI.
What is the problem with modern day education? :)
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top