Python 什么时候使用 zip 而不是 izip 更好?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4989763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When is it better to use zip instead of izip?
提问by Neil G
When is it better to use zipinstead of itertools.izip?
什么时候用zip而不是更好itertools.izip?
采纳答案by Amber
When you know you'll want the full list of items constructed (for instance, for passing to a function that would modify that list in-place). Or when you want to force the arguments you're passing to zip()to be completely evaluated at that specific point.
当您知道您需要构造完整的项目列表时(例如,用于传递到将就地修改该列表的函数)。或者当你想强制你传递的参数zip()在那个特定点被完全评估时。
回答by Ignacio Vazquez-Abrams
In 2.x, when you needa list instead of an iterator.
在 2.x 中,当您需要列表而不是迭代器时。
回答by Don
zipcomputes all the list at once, izipcomputes the elements only when requested.
zip一次计算所有列表,izip仅在请求时计算元素。
One important difference is that 'zip' returns an actual list, 'izip' returns an 'izip object', which is not a list and does not support list-specific features (such as indexing):
一个重要的区别是 'zip' 返回一个实际列表,'izip' 返回一个 'izip object',它不是一个列表并且不支持特定于列表的功能(例如索引):
>>> l1 = [1, 2, 3, 4, 5, 6]
>>> l2 = [2, 3, 4, 5, 6, 7]
>>> z = zip(l1, l2)
>>> iz = izip(l1, l2)
>>> isinstance(zip(l1, l2), list)
True
>>> isinstance(izip(l1, l2), list)
False
>>> z[::2] #Get odd places
[(1, 2), (3, 4), (5, 6)]
>>> iz[::2] #Same with izip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'itertools.izip' object is unsubscriptable
So, if you need a list (an not a list-like object), just use 'zip'.
因此,如果您需要一个列表(不是类似列表的对象),只需使用“zip”。
Apart from this, 'izip' can be useful for saving memory or cycles.
除此之外,'izip' 可用于节省内存或周期。
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 zipwould have computed all(a, b)couples before entering the cycle.
usingzip会在进入循环之前计算所有(a, b)对。
Moreover, if lst_aand lst_bare very large (e.g. millions of records), zip(a, b)will build a third list with double space.
此外,如果lst_a和lst_b非常大(例如数百万条记录),zip(a, b)将构建具有双倍空间的第三个列表。
But if you have small lists, maybe zipis faster.
但是如果你有小名单,也许zip会更快。
回答by Sakib Ahammed
The itertools library provides "iterators" for common Python functions. From the itertools docs, "Like zip() except that it returns an iterator instead of a list." The I in izip() means "iterator".
itertools 库为常见的 Python 函数提供了“迭代器”。来自 itertools 文档,“像 zip() 不同之处在于它返回一个迭代器而不是一个列表。” izip() 中的 I 表示“迭代器”。
Python iterators are a "lazy loaded" sequence that saves memory over regular in-memory list. So, you would use itertools.izip(a, b) when the two inputs a, b are too big to keep in memory at one time.
Python 迭代器是一个“延迟加载”序列,它比常规内存列表节省内存。因此,当两个输入 a, b 太大而无法一次保存在内存中时,您将使用 itertools.izip(a, b) 。
Look up the Python concepts related to efficient sequential processing:
查找与高效顺序处理相关的 Python 概念:
"generators" & "yield"
"iterators"
"lazy loading"

