python itertools模块

1. izip和zip的区别

zip computes all the list at once, izip computes the elements only when requested:

zip 一次性计算集合中的所有元素,izip 只有当需要的时候才计算元素

the latter can be useful for saving memory or cycles.

izip 在节省内存和循环中比zip好很多

E.g. the following code may exit after few cycles, so there is no need to compute all items of combined list:

lst_a = ... #list with very large number of items
lst_b = ... #list with very large number of items
#At each cycle, the next couple is provided
for a, b in izip(lst_a, lst_b):
    if a == b:
        break
print a

using zip would have computed all (a, b) couples before entering the cycle.

使用zip在进入循环之前,将计算所有list_a, list_b中的元素

Moreover, if lst_a and lst_b are very large (e.g. millions of records),zip(a, b) will build a third list with double space.

而且如果list_a和list_b 中元素数量过大,zip将建立第三个列表进行保存,则占用双倍内存

But if you have small lists, maybe zip is faster.

如果元素数量小,zip可能更快速


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值