473,378 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2449
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bliss | last post by:
Hi All, I have a table Truck_Journey with the following structure Tj_Date Date Tj_Truck_Id Number(5) Tj_Truck_No Varchar2(10) Tj_Km Number(9) ....
25
by: sql_server_2000_user | last post by:
Hi, I have a table with about 305 million rows, and a composite primary key that consists of an ascending int and an ascending varchar(18), which is typically of length 13. Even if all the keys...
8
by: Fan Zhang | last post by:
Dear group, I wrote a simple code trying to use the integer index of a "for" loop inside the loop. The idea is to convert it to double type and lead it into a function. However it appears I...
6
by: BB | last post by:
Hello all, I am using the currency manager in VB to navigate a dataset. I know how to use .position to loop through the rows, but what I want to do is *get* the position of the row I just...
3
by: Brian Piotrowski | last post by:
Hi All, I've probably done this before, but for the life of me I can't remember how I did it. I need to move values from a DB table into an array to be used for other queries. The number of...
1
by: BeanCounterCPA | last post by:
OK, I'm sure there is an easy way to do this, but I am just a novice at Access and can not find a solution. I have a database that I used to pull a confirmation sample from by using dollar unit...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
1
by: sksksk | last post by:
I want to achieve the following process in the smarty for $item one i should be able to get the value using loop.index, but without any luck. any help is appreciated. <?php for ($i = 1; $i...
3
by: Robert Bevington | last post by:
Hi all, I ran into memory problems while tying to search and replace a very large text file. To solve this I break the file up into chunks and run the search and replace on each chunk. This...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.