标签云

微信群

扫码加入我们

WeChat QR Code

How do I access the index itself for a list like the following?ints = [8, 23, 45, 12, 78]When I loop through it using a for loop, how do I access the loop index, from 1 to 5 in this case?


Note that indexes in python start from 0, so the indexes for your example list are 0 to 4 not 1 to 5

2019年07月24日55分16秒

As Aaron points out below, use start=1 if you want to get 1-5 instead of 0-4.

2019年07月24日55分16秒

The question was about list indexes; since they start from 0 there is little point in starting from other number since the indexes would be wrong (yes, the OP said it wrong in the question as well). Otherwise, calling the variable that is tuple of index, item just index is very misleading, as you noted. Just use for index, item in enumerate(ints).

2019年07月23日55分16秒

Better is to enclose index inside parenthesis pairs as (index), it will work on both the Python versions 2 and 3.

2019年07月24日55分16秒

AnttiHaapala The reason, I presume, is that the question's expected output starts at index 1 instead 0

2019年07月24日55分16秒

That should probably be xrange for pre-3.0.

2019年07月24日55分16秒

Use enumerate instead

2019年07月23日55分16秒

For Python 2.3 above, use enumerate built-in function since it is more Pythonic.

2019年07月23日55分16秒

Enumerate is not always better - it depends on the requirements of the application. In my current situation the relationships between the object lengths is meaningful to my application. Although I started out using enumerate, I switched to this approach to avoid having to write logic to select which object to enumerate.

2019年07月23日55分16秒

adg I don't see how avoid enumerate saves any logic; you still have to select which object to index with i, no?

2019年07月23日55分16秒

This is not wrong and is used in C/C++ and others. It's considered as non-pythonic, but can also be used in python. Like simple solutions that break it down to the source :+

2019年07月24日55分16秒

Where does it say it's wrong?

2019年07月24日55分16秒

Some python extremists would say, don't do this. But I said it only to indicate that there is more than one possible way

2019年07月24日55分16秒

"readability counts"The speed difference in the small <1000 range is insignificant.It is 3% slower on an already small time metric.

2019年07月24日55分16秒

How about updating the answer to Python 3?

2019年07月23日55分16秒

This won't work for iterating through generators. Just use enumerate().

2019年07月24日55分16秒

Nowadays, the current idiom is enumerate, not the range call.

2019年07月24日55分16秒

and it's the same as the older answer: stackoverflow.com/a/522576/6451573

2019年07月23日55分16秒

Note that the first option should not be used, since it only works correctly only when each item in the sequence is unique.

2019年07月24日55分16秒

First option is O(n²), a terrible idea. If your list is 1000 elements long, it'll take literally a 1000 times longer than using enumerate. You should delete this answer.

2019年07月24日55分16秒

This will break down if there are repeated elements in the list as index() will search for the first occurrence of x, not mentioning the O( n^2 ) time required to look up each element.

2019年07月24日55分16秒

your solution works with: list1 = [8, 8, 8, 8, 8] ?

2019年07月24日55分16秒

totally agreed that it won't work for duplicate elements in the list. afterall I'm also learning python.

2019年07月24日55分16秒