在循环中查找列表的索引 [英] Finding the index of a list in a loop
本文介绍了在循环中查找列表的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的问题.如果我在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):
...
这篇关于在循环中查找列表的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文