如何将一个元组的Python生成器拆分为2个单独的生成器? [英] How to split a Python generator of tuples into 2 separate generators?

查看:82
本文介绍了如何将一个元组的Python生成器拆分为2个单独的生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大致如下的生成器:

I have a generator that is roughly as follows:

def gen1():
    for x, y in enumerate(xrange(20)):
        a = 5*x
        b = 10*y
        yield a, b

我要从此生成器中创建2个单独的生成器,如下所示:

From this generator, I would like to create 2 separate generators as follows:

for a in gen1_split_a():
    yield a

for b in gen1_split_b():
    yield b

我在玩什么,SA?

推荐答案

您不能,不仅仅因为能够在第二个循环中生成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

或更妙的是,只需在一个循环中同时处理ab.

or better yet, just process both a and b in the one loop.

这篇关于如何将一个元组的Python生成器拆分为2个单独的生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆