更新时间:2022-05-25 23:00:38
您不能,不仅仅因为能够在第二个循环中生成b
值而保留所有生成器输出.就内存而言,这可能会付出高昂的代价.
You can't, not without ending up holding all generator output just to be able to produce b
values in the second loop. That can get costly in terms of memory.
您将使用 itertools.tee()
来复制生成器:
You'd use itertools.tee()
to 'duplicate' the generator:
from itertools import tee
def split_gen(gen):
gen_a, gen_b = tee(gen, 2)
return (a for a, b in gen_a), (b for a, b in gen_b)
gen1_split_a, gen1_split_b = split_gen(gen1)
for a in gen1_split_a:
print a
for b in gen1_split_b:
print b
,但是在这种情况下,发生的情况是tee
对象最终将不得不存储 gen1
产生的所有东西.从文档中:
but what happens in this case is that the tee
object will end up having to store everything gen1
produces. From the documentation:
此itertool可能需要大量辅助存储(取决于需要存储多少临时数据).通常,如果一个迭代器在另一个迭代器启动之前使用了大部分或全部数据,则使用
list()
而不是tee()
会更快.
根据该建议,只需将b
值放入第二个循环的列表中即可:
Following that advice, just put the b
values into a list for the second loop:
b_values = []
for a, b in gen1():
print a
b_values.append(a)
for b in b_values:
print b
或更妙的是,只需在一个循环中同时处理a
和b
.
or better yet, just process both a
and b
in the one loop.