Basic question

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun May 13 20:43:50 EDT 2007


En Sat, 12 May 2007 20:13:48 -0300, Alex Martelli <aleax at mac.com> escribió:

> Cesar G. Miguel <cesar.gomes at gmail.com> wrote:
>> -------------------
>> 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 :)

-- 
Gabriel Genellina




More information about the Python-list mailing list