Basic question

Cesar G. Miguel cesar.gomes at gmail.com
Sat May 12 14:47:11 EDT 2007


On May 12, 3:40 pm, Dmitry Dzhus <m... at sphinx.net.ru> wrote:
> > Actually I'm trying to convert a string to a list of float numbers:
> > str = '53,20,4,2' to L = [53.0, 20.0, 4.0, 2.0]
>
> str="53,20,4,2"
> map(lambda s: float(s), str.split(','))
>
> Last expression returns: [53.0, 20.0, 4.0, 2.0]
> --
> Happy Hacking.
>
> Dmitry "Sphinx" Dzhushttp://sphinx.net.ru

Nice!

The following also works using split and list comprehension (as
suggested in a brazilian python forum):

-------------------
L = []
file = ['5,1378,1,9', '2,1,4,5']
str=''
for item in file:
	L.append([float(n) for n in item.split(',')])
-------------------

Thank you for all suggestions!




More information about the Python-list mailing list