python顺序表转置_懒惰地转置Python中的列表

So, I have an iterable of 3-tuples, generated lazily. I'm trying to figure out how to turn this into 3 iterables, consisting of the first, second, and third elements of the tuples, respectively. However, I wish this to be done lazily.

So, for example, I wish [(1, 2, 3), (4, 5, 6), (7, 8, 9)] to be turned into [1, 4, 7], [2, 5, 8], [3, 6, 9]. (Except I want iterables not lists.)

The standard zip(*data) idiom doesn't work, because the argument unpacking expands the entire iterable. (You can verify this by noting that zip(*((x, x+1, x+2) for x in itertools.count(step=3))) hangs.)

The best I've come up with thus far is the following:

def transpose(iterable_of_three_tuples):

teed = itertools.tee(iterable_of_three_tuples, 3)

return map(lambda e: e[0], teed[0]), map(lambda e: e[1], teed[1]), map(lambda e: e[2], teed[2])

This seems to work. But it hardly seems like clean code. And it does a lot of what seems to be unnecessary work.

解决方案

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] )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值