Re: can I get the index number in for x in y loop?

2006-04-03 Thread Fuzzyman
JuHui wrote: > >>> a='String' > >>> for x in a: > ... print x > ... > S > t > r > i > n > g > >>> > > can I get the index number of a in the upon loop within for x in a > loop? Although enumerate is the 'right' answer, I personally prefer : i = 0 while i < len(some_sequence): val = som

Re: can I get the index number in for x in y loop?

2006-04-03 Thread John Hunter
> "Scott" == Scott David Daniels <[EMAIL PROTECTED]> writes: Scott> I cannot find the distance in meters between Paris and Scott> London with: for i in range(10): print i Works for me def range(x): yield '332.8 km' for i in range(10): print i ...may not be considered b

Re: can I get the index number in for x in y loop?

2006-04-03 Thread Scott David Daniels
JuHui wrote: > which one has best performance? > > a:for i in range(0,len(a)) > b:for x in a > c:for x,y in enumerate(a) Read up on the timeit module and figure it out for yourself. The answer will depend on the distribution of your data. > but, it seems I can't get index number with b format..

Re: can I get the index number in for x in y loop?

2006-04-03 Thread JuHui
thanks a lot! :) -- http://mail.python.org/mailman/listinfo/python-list

Re: can I get the index number in for x in y loop?

2006-04-03 Thread Felipe Almeida Lessa
Em Seg, 2006-04-03 às 08:47 -0700, JuHui escreveu: > which one has best performance? Let's see... > a:for i in range(0,len(a)) $ python2.4 -mtimeit -s 'a=[None]*100' 'for i in range(len(a)): j = a[i] ' 10 loops, best of 3: 17.7 usec per loop $ python2.4 -mtimeit -s 'a=[None]*100' 'for i

Re: can I get the index number in for x in y loop?

2006-04-03 Thread JuHui
which one has best performance? a:for i in range(0,len(a)) b:for x in a c:for x,y in enumerate(a) but, it seems I cann't get index number with b format..:( -- http://mail.python.org/mailman/listinfo/python-list

Re: can I get the index number in for x in y loop?

2006-04-03 Thread Rune Strand
JuHui wrote: > >>> a='String' > >>> for x in a: > ... print x > ... > S > t > r > i > n > g > >>> > > can I get the index number of a in the upon loop within for x in a > loop? for x, y in enumerate(a) print x, y -- http://mail.python.org/mailman/listinfo/python-list

Re: can I get the index number in for x in y loop?

2006-04-03 Thread Paul McGuire
"JuHui" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > >>> a='String' > >>> for x in a: > ... print x > ... > S > t > r > i > n > g > >>> > > can I get the index number of a in the upon loop within for x in a > loop? > Use enumerate. See example below. -- Paul >>> a = "Strin

Re: can I get the index number in for x in y loop?

2006-04-03 Thread Luigi
Try this: >>> a='String' >>> i=0 >>> for x in a: ... print i, x ... i+=1 ... 0 S 1 t 2 r 3 i 4 n 5 g -- http://mail.python.org/mailman/listinfo/python-list

can I get the index number in for x in y loop?

2006-04-03 Thread JuHui
>>> a='String' >>> for x in a: ... print x ... S t r i n g >>> can I get the index number of a in the upon loop within for x in a loop? -- http://mail.python.org/mailman/listinfo/python-list