Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert comma separated string of floats into list?

I need to define a function txtnum(L) that takes a string of comma separated floats such as "1.5,2.5,3.5" as a parameter and converts it into a list [1.5, 2.5, 3.5].

I have tried using .split(), .join(), map(), etc and cannot get anything to return a list that does NOT include quotations. I'm pretty new to Python and a little lost here.

How would I go about doing this?

like image 268
E.T. Avatar asked Feb 17 '16 07:02

E.T.


1 Answers

You need to convert the datatype of splitted vars because splitting alone string gives you a list of strings.

>>> s = "1.5,2.5,3.5"
>>> [float(i) for i in s.split(',')]
[1.5, 2.5, 3.5]
>>> 

or

>>> map(float, s.split(','))
[1.5, 2.5, 3.5]
like image 117
Avinash Raj Avatar answered Nov 15 '22 04:11

Avinash Raj