使用索引迭代或访问 python 生成器

Iterating or accessing a python generator with index

是否可以像 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

你熟悉enumerate函数吗?

for i, value in enumerate(your_gen):
    print(i)
    print(value)