Python Forum
Convert multiple decimal numbers in string to floats - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Convert multiple decimal numbers in string to floats (/thread-28984.html)



Convert multiple decimal numbers in string to floats - Katy8 - Aug-12-2020

I'm going through the MIT Open Course Ware in my free time. Just want to begin learning, so this is not for a class or credit. Just need a nudge in the right direction, not necessarily a spelled out answer.

I'm trying to figure out how to convert a string to a float. For example:

s = "1.2, 3.4, 5.6"

I know that there is a way to do it that is probably very clever (I may not be quite as smart as I thought to take the MIT course!). Wondering if anyone else is familiar with this conundrum? Wall


RE: Convert multiple decimal numbers in string to floats - Intr0spective - Aug-12-2020

Hi,

Are you trying to get 3 floats out of it? 1.2 then 3.4 and finally 5.6
Or any float e.g 1.23456?


RE: Convert multiple decimal numbers in string to floats - jefsummers - Aug-12-2020

First you will need to split the string on the commas. That gives you a list of strings. Then, iterate through the list converting the strings to floats.

There is a really cool Python construct called a list comprehension that works really well on the string list to float list conversion.


RE: Convert multiple decimal numbers in string to floats - Katy8 - Aug-15-2020

(Aug-12-2020, 06:47 PM)Intr0spective Wrote: Hi,

Are you trying to get 3 floats out of it? 1.2 then 3.4 and finally 5.6
Or any float e.g 1.23456?

Yes to your first question, no to your second question.

(Sorry for the delay in my response!)
Thanks!


RE: Convert multiple decimal numbers in string to floats - buran - Aug-15-2020

so what have you tried so far? you did try something, right?


RE: Convert multiple decimal numbers in string to floats - Intr0spective - Aug-16-2020

(Aug-15-2020, 06:37 PM)Katy8 Wrote:
(Aug-12-2020, 06:47 PM)Intr0spective Wrote: Hi,

Are you trying to get 3 floats out of it? 1.2 then 3.4 and finally 5.6
Or any float e.g 1.23456?

Yes to your first question, no to your second question.

(Sorry for the delay in my response!)
Thanks!

Katy8,

Then as jefsummers advised:

1.) split the string "1.2, 3.4, 5.6" on commas, that means you pass a ',' as a parameter to split() method. This gives you a list of strings. If you do it right then the list should look like ['1.2', '3.4', '5.6']
2.) convert each value in this string to float. To do that you have to loop/iterate over each value in the list converting it to float - you may print it or store it again in list - depends on what you plan to do with it. E.g if storing it in list then the list should look like
[1.2, 3.4, 5.6]

Lastly, if you post your code folks here will tell you what is wrong with it rather than
coding it for you Wink


RE: Convert multiple decimal numbers in string to floats - Katy8 - Aug-16-2020

Thanks to both of you. I'm not sure if this is the way that the authors intended, but I may just move on and try to go back to it later.

Thanks also for the general posting tip!