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可能更快速