Basic question

G

Gabriel Genellina

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

The assignment to str is useless (in fact potentially damaging because
you're hiding a built-in name).

L = [float(n) for item in file for n in item.split(',')]

is what I'd call Pythonic, personally (yes, the two for clauses need to
be in this order, that of their nesting).

But that's not the same as requested - you get a plain list, and the
original was a list of lists:

L = [[float(n) for n in item.split(',')] for item in file]

And thanks for my "new English word of the day": supererogatory :)
 
A

Alex Martelli

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

The assignment to str is useless (in fact potentially damaging because
you're hiding a built-in name).

L = [float(n) for item in file for n in item.split(',')]

is what I'd call Pythonic, personally (yes, the two for clauses need to
be in this order, that of their nesting).

But that's not the same as requested - you get a plain list, and the
original was a list of lists:

L = [[float(n) for n in item.split(',')] for item in file]

Are we talking about the same code?! What I saw at the root of this
subthread was, and I quote:
L = []
file = ['5,1378,1,9', '2,1,4,5']
str=''
for item in file:
j=0
while(j<len(item)):
while(item[j] != ','):
str+=item[j]
j=j+1
if(j>= len(item)): break

if(str != ''):
L.append(float(str))
str = ''

j=j+1

print L

This makes L a list of floats, DEFINITELY NOT a list of lists.
And thanks for my "new English word of the day": supererogatory :)

You're welcome! Perhaps it's because I'm not a native speaker of
English, but I definitely do like to widen my vocabulary (and others').


Alex
 
G

Gabriel Genellina

Are we talking about the same code?! What I saw at the root of this
subthread was, and I quote:

[...code painfully building a plain list...]

Oh, sorry, I got confused with another reply then.
You're welcome! Perhaps it's because I'm not a native speaker of
English, but I definitely do like to widen my vocabulary (and others').

Me too!
 
M

Max M

Dmitry Dzhus skrev:
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]

The lambda is not needed there, as float is a callable.

map(float, str.split(','))

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top