什么时候使用 zip 代替 izip 更好?

发布于 2024-10-17 09:43:51 字数 228 浏览 4 评论 0 原文

什么时候使用 zip 而不是 itertools.izip

When is it better to use zip instead of itertools.izip?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲欢浪云 2024-10-24 09:43:51

zip 一次性计算所有列表,izip 仅在请求时计算元素。

一个重要的区别是“zip”返回一个实际列表,“izip”返回一个“izip 对象”,它不是列表并且不支持特定于列表的功能(例如索引):

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

因此,如果您需要一个列表(不是类似列表的对象),只需使用“zip”。

除此之外,“izip”对于节省内存或周期很有用。

例如,以下代码可能会在几个周期后退出,因此无需计算组合列表的所有项目:

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

使用 zip 将计算 all (a, b ) 进入循环之前进行配对。

此外,如果lst_alst_b非常大(例如数百万条记录),zip(a, b)将使用 double 构建第三个列表空间。

但如果您的列表较小,也许 zip 会更快。

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

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

>>> 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'.

Apart from this, 'izip' can be useful for saving memory or cycles.

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.

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.

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

谎言 2024-10-24 09:43:51

当您知道您需要构建的完整项目列表时(例如,传递给会就地修改该列表的函数)。或者,当您想要强制传递给 zip() 的参数在该特定点进行完全评估时。

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.

好听的两个字的网名 2024-10-24 09:43:51

itertools 库为常见的 Python 函数提供“迭代器”。从 itertools 文档来看,“与 zip() 类似,只是它返回一个迭代器而不是列表。” izip() 中的 I 表示“迭代器”。

Python 迭代器是一个“延迟加载”序列,它比常规内存列表节省内存。因此,当两个输入 a、b 太大而无法同时保留在内存中时,您可以使用 itertools.izip(a, b)。

查找与高效顺序处理相关的Python概念:

"generators" & "yield"
"iterators"
"lazy loading"

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".

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.

Look up the Python concepts related to efficient sequential processing:

"generators" & "yield"
"iterators"
"lazy loading"
灵芸 2024-10-24 09:43:51

在 2.x 中,当您需要一个列表而不是迭代器时。

In 2.x, when you need a list instead of an iterator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文