Basic question

Karlo Lozovina _karlo_ at _mosor.net
Sat May 12 14:09:46 EDT 2007


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.



More information about the Python-list mailing list