同时从两个生成器 yield 生成一个元组

yield from two generators at the same time, and make the result a tuple

我想知道是否有 pythonic 方式同时展开两个生成器:

例如我有两个行数相同的文件。当然,我可以在阅读全部内容后压缩这些行。

但是否可以同时从两个生成器生成元素? 当我尝试 运行 这样的代码时,它会抱怨:

return (yield from test, yield from predict)
                       ^
SyntaxError: invalid syntax

这里,testpredict是这样打开两个文件得到的两个生成器:

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)

我可能是误会了,听起来你在找 zip()。你可以这样做:

with open(test_filename,"rt") as test:
    with open(predict_filename,"rt") as predict:
        for couple in zip(test,predict):
            do_something(couple)