在循环中查找列表的索引

问题描述:

我有一个简单的问题.如果我在python中有一个for循环,如下所示:

I have a simple question. If I have a for loop in python as follows:

for name in nameList:

我怎么知道元素名称的索引是什么?我知道我可以做些类似的事情:

How do I know what the index is for the element name? I know I can some something like:

i = 0
for name in nameList:
    i= i + 1
    if name == "something":
        nameList[i] = "something else"

我只是觉得应该有一种更具可读性的方式来做到这一点...

I just feel there should be a more readable way of doing this...

使用内置函数

Use the built in function enumerate.

for index, name in enumerate(nameList):
    ...