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

J

JuHui

a='String'.... 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?
 
P

Paul McGuire

JuHui said:
... 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
.... print x
....
S
t
r
i
n
g.... print x
....
(0, 'S')
(1, 't')
(2, 'r')
(3, 'i')
(4, 'n')
(5, 'g')
 
R

Rune Strand

JuHui said:
... 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
 
J

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..:(
 
F

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
'
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
'
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,
 
S

Scott David Daniels

JuHui said:
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
(e-mail address removed)
 
J

John Hunter

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
 
F

Fuzzyman

JuHui said:
... 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 += 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top