from itertools import tee
#use the tee() function to create another version of iterator. here, it's result_backup
result, result_backup = tee(result)
print("**first iterate**")
for i, r in enumerate(result):
print(r)
print("**second iterate**")
#in the 2nd time, you can use result_backup
for i, r in enumerate(result_backup):
print(r)
这是python中
iterator
的默认行为如果希望迭代器在第二次运行时仍能工作,可以使用itertools.tee()函数创建迭代器的第二个版本。如下图所示:
相关问题 更多 >
编程相关推荐