Lazily transpose a list in Python

浪子不回头ぞ 提交于 2019-12-04 02:34:25

Your transpose is pretty much exactly what you need.

With any solution you'd choose, you'd have to buffer the unused values (e.g. to get to the 7, you have to read 1-6, and store them in memory for when the other iterables ask for them). tee already does exactly that kind of buffering, so there's no need implementing it yourself.

The only other (minor) thing is that I'd write it slightly differently, avoiding the map and lambdas:

def transpose(iterable_of_three_tuples):
    teed = itertools.tee(iterable_of_three_tuples, 3)
    return ( e[0] for e in teed[0] ),  ( e[1] for e in teed[1] ),  ( e[2] for e in teed[2] )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!