【发布时间】:2021-07-03 21:03:31
【问题描述】:
是否可以像list 一样使用index 迭代python generator 或通过index 访问generator 元素
例如:
sample_list = [1,2,3,4]
for i in range(len(sample_list)): #iterating with index
print (sample_list[i]) #accessing element through index
【问题讨论】:
-
@Brian 感谢您的回复。它更多的是使用给定的索引访问它,我希望使用
index遍历它。我会检查我是否可以使用它。 -
您能评论一下为什么要索引生成器吗?您想在使用索引时访问它吗?还是您想跳过前 n-1 个产生的值以立即获得第 n 个?还是别的什么?
-
@Brian 检查索引
i处的value,如果它证明condition我想访问i+1或跳过i+1并转到i+2
标签: python python-3.x generator