Basic question

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


On May 12, 2:45 pm, Basilisk96 <basilis... at gmail.com> wrote:
> On May 12, 12:18 pm, "Cesar G. Miguel" <cesar.go... at gmail.com> wrote:
>
>
>
> > I've been studying python for 2 weeks now and got stucked in the
> > following problem:
>
> > for j in range(10):
> >   print j
> >   if(True):
> >        j=j+2
> >        print 'interno',j
>
> > What happens is that "j=j+2" inside IF does not change the loop
> > counter ("j") as it would in C or Java, for example.
>
> > Am I missing something?
>
> > []'s
> > Cesar
>
> What is your real intent here? This is how I understand it after
> reading your post: you want to create a loop that steps by an
> increment of 2. If that's the case, then:
>
> >>> for j in range(0,10,2):
>
> ...     print j
> ...
> 0
> 2
> 4
> 6
> 8
>
> would be a simple result.
>
> Cheers,
> -Basilisk96

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]

As some of you suggested, using while it works:

-------------------------------------
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 :-)

Thanks for all suggestions!




More information about the Python-list mailing list