Reputation: 39906
I'm trying to know if there's a pythonic way to unroll two generators at the same time:
e.g. I have two files with the same number of lines. Of course, I could zip the lines after reading their entire content.
But is it possible to yield elements from the two generators at the same time ? when I try to run such code, it complains:
return (yield from test, yield from predict)
^
SyntaxError: invalid syntax
here, test
and predict
are two generators obtained by opening two files this way :
with open(test_filename,"rt") as test:
with open(predict_filename,"rt") as predict:
for couple in yield_couples(test,predict):
do_something(couple)
def yield_couples(test,predict,category):
return (yield from test, yield from predict)
Upvotes: 0
Views: 578
Reputation: 1785
I might be misunderstanding, but it sounds like you're looking for zip(). You can do:
with open(test_filename,"rt") as test:
with open(predict_filename,"rt") as predict:
for couple in zip(test,predict):
do_something(couple)
Upvotes: 1