我正在寻找一种使用索引值而不是实际元素本身遍历列表的 python 方法。代码正是这样做的:
for index in range(len(list)-1):
# do stuff
我正在查看是否已经有执行此操作的函数,或者我是否应该自己添加它。
最佳答案
使用枚举
:
for index, ignore_this_its_the_value in enumerate(list):
# do stuff
但实际上:
for index in range(len(list)):
# do stuff
确实会产生适当的索引。
关于python - 是否有内置的 python 方法使用索引值而不是元素遍历列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37199448/