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可能更快速
本文对比了zip与izip在处理大量数据时的性能差异,重点在于它们如何节省内存和提高循环效率。通过具体示例说明了在不同场景下选择使用zip还是izip的最佳实践。
1062

被折叠的 条评论
为什么被折叠?



