Basic question
Alex Martelli
aleax at mac.com
Mon May 14 01:05:47 EDT 2007
Gabriel Genellina <gagsl-py2 at yahoo.com.ar> wrote:
> 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]
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
More information about the Python-list
mailing list