Basic question

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


On May 12, 3:09 pm, Karlo Lozovina <_karlo_ at _mosor.net> wrote:
> Cesar G. Miguel wrote:
> > -------------------------------------
> > 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
> > But I'm not sure this is an elegant pythonic way of coding :-)
>
> Example:
>
> In [21]: '5,1378,1,9'.split(',')
> Out[21]: ['5', '1378', '1', '9']
>
> So, instead of doing that while-based traversal and parsing of `item`,
> just split it like above, and use a for loop on it. It's much more
> elegant and pythonic.
>
> HTH,
> Karlo.

Great! Now it looks better :-)




More information about the Python-list mailing list