469,317 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,317 developers. It's quick & easy.

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

>>> 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?

Apr 3 '06 #1
9 2327
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

Apr 3 '06 #2
"JuHui" <ph*****@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
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 = "String"
for x in a: .... print x
....
S
t
r
i
n
g for x in enumerate(a): .... print x
....
(0, 'S')
(1, 't')
(2, 'r')
(3, 'i')
(4, 'n')
(5, 'g')

Apr 3 '06 #3

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

Apr 3 '06 #4
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..:(

Apr 3 '06 #5
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]
'
100000 loops, best of 3: 17.7 usec per loop

$ python2.4 -mtimeit -s 'a=[None]*100' 'for i in xrange(len(a)):
j = a[i]
'
100000 loops, best of 3: 16.8 usec per loop
b:for x in a
$ python2.4 -mtimeit -s 'a=[None]*100' 'i = 0
for j in a:
i += 1
'
100000 loops, best of 3: 15.7 usec per loop
c:for x,y in enumerate(a)


$ python2.4 -mtimeit -s 'a=[None]*100' 'for i, j in enumerate(a):
pass
'
100000 loops, best of 3: 12.9 usec per loop
Using enumerate is cleaner and faster.

HTH,

--
Felipe.

Apr 3 '06 #6
thanks a lot!
:)

Apr 3 '06 #7
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..:(


Well, that's true, but it is a bit like saying:

I cannot find the distance in meters between Paris and London with:
for i in range(10):
print i

--Scott David Daniels
sc***********@acm.org
Apr 3 '06 #8
>>>>> "Scott" == Scott David Daniels <sc***********@acm.org> 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 best practice, though <wink>

JDH

Apr 3 '06 #9

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 = some_sequence[i]
...
i += 1

This is so that I can manually wind 'i' backwards and forwards manually
from other parts of the code.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Apr 3 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

25 posts views Thread by sql_server_2000_user | last post: by
8 posts views Thread by Fan Zhang | last post: by
3 posts views Thread by Brian Piotrowski | last post: by
1 post views Thread by BeanCounterCPA | last post: by
1 post views Thread by sksksk | last post: by
3 posts views Thread by Robert Bevington | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.