Loop over two generator together

ε祈祈猫儿з 提交于 2019-11-29 17:01:30

问题


I have two generators say A() and B(). I want to iterate over both the generators together. Something like:

for a,b in A(),B():    # I know this is wrong
    #do processing on a and b

One way is to store the results of both the functions in lists and then loop over the merged list. Something like this:

resA = [a for a in A()]
resB = [b for b in B()]
for a,b in zip(resA, resB):
    #do stuff

If you are wondering, then yes both the functions yield equal number of value.

But I can't use this approach because A()/B() returns so many values. Storing them in a list would exhaust the memory, that's why I am using generators.

Is there any way to loop over both the generators at once?


回答1:


You were almost there. In Python 3, just pass the generators to zip():

for a, b in zip(A(), B()):

zip() takes any iterable, not just lists. It will consume the generators one by one.

In Python 2, use itertools.izip():

from itertools import izip

for a, b in izip(A(), B()):

As an aside, turning a generator into a list is as simple as list(generator); no need to use a list comprehension there.




回答2:


Sounds like you want itertools.izip:

from itertools import izip

for a, b in izip(A(), B()):

From the docs:

Like zip() except that it returns an iterator instead of a list.

So this way you never create a list, either of A(), B() or the izip().

Note that in Python 3's basic zip is like Python 2.x's itertools.izip.




回答3:


You can use the zip function to transform the two lists generated into an list of tuples, so you can use the way you want:

for a,b in zip(A(), B()):
    pass


来源:https://stackoverflow.com/questions/20910213/loop-over-two-generator-together

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!