[Python]zip 和 izip , izip_longest比较

zip是build-in方法

而izip是itertools中的一个方法


这两个方法的作用是相似的,但是具体使用中有什么区别呢?今天来探究一下。

zip

文档中这样描述:

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence.

就是把多个序列或者是迭代器的元素,组合成元组。返回的元组的长度是所有输入序列中最短的

In [27]: a = ['a', 'b', 'c', 'd', 'e']

In [28]: b = range(10)

In [29]: zip(a,b)
Out[29]: [('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)]

组合之后的元组长度是依照两个输入序列中最短的a为准的。


如果输入的两个序列都是特别大的情况,zip就会很慢了。使用izip比较下。

In [30]: a = range(10000000)

In [31]: b = range(10000000)

In [32]: tim
%%timeit %time    %timeit

In [32]: %timeit(zip(a,b))
1 loops, best of 3: 811 ms per loop

In [33]: import itertools

In [34]: %timeit(itertools.izip(a,b))
1000000 loops, best of 3: 349 ns per loop

这样看izip会快的多。


izip

文档中的描述:

Make an iterator that aggregates elements from each of the iterables. Like zip() except that it returns an iterator instead of a list. Used for lock-step iteration over several iterables at a time.

把不同的迭代器的元素聚合到一个迭代器中。类似zip()方法,但是返回的是一个迭代器而不是一个list。用于同步迭代一次几个iterables

orangleliu: 因为返回的是一个迭代器,并且同步迭代,所以速度比较快。


izip_longest

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted

也就是说这个zip方法使用izip一样的原理,但是会使用最长的迭代器来作为返回值的长度,并且可以使用fillvalue来制定那些缺失值的默认值

In [35]: a = ['a','b','c']

In [36]: b = range(10)

In [37]: itertools.izip_longest(a,b,fillvalue=-1)
Out[37]: <itertools.izip_longest at 0x250e540>

In [38]: c = itertools.izip_longest(a,b,fillvalue=-1)

In [42]: for i in c:
   ....:     print i
   ....:
('a', 0)
('b', 1)
('c', 2)
(-1, 3)
(-1, 4)
(-1, 5)
(-1, 6)
(-1, 7)
(-1, 8)
(-1, 9)


探究一下,基本的区别就是这些,具体的使用要看具体的编程场景。


### Python 中 `zip_longest` 未定义的解决方案 在 Python 的不同版本中,`itertools.zip_longest()` 函数的行为有所不同。如果遇到 `zip_longest not defined` 错误,则可能是由于使用的 Python 版本不支持该方法或者导入方式有误。 #### 解决方案一:确认 Python 版本 在 Python 3.x 中,`zip_longest` 是作为 `itertools` 模块的一部分引入的。而在 Python 2.x 中,并不存在名为 `zip_longest` 的函数,取而代之的是 `izip_longest`[^1]。因此,在使用前需确保运行环境为 Python 3.x 并正确导入模块: ```python from itertools import zip_longest ``` #### 解决方案二:检查是否正确导入 即使是在 Python 3.x 下,也需要显式地从 `itertools` 导入 `zip_longest` 才能正常使用它。如果没有正确导入,就会引发 `NameError: name 'zip_longest' is not defined` 错误。以下是正确的导入语句以及用法示例: ```python from itertools import zip_longest lists = ['abc', range(5)] for item in zip_longest(*lists, fillvalue='-'): print(item) ``` 上述代码会输出如下结果: ``` ('a', 0) ('b', 1) ('c', 2) ('-', 3) ('-', 4) ``` 这里的关键在于通过参数 `fillvalue` 来指定当较短序列结束后的填充值,默认情况下这个值为空白字符(None)。这使得即便两个可迭代对象长度不一样也能正常配对处理。 #### 解决方案三:兼容性考虑 (针对旧版 Python 或未知版本场景) 为了使程序能够同时适用于 Python 2 Python 3,可以采用条件判断的方式来动态选择合适的工具函数: ```python try: from itertools import izip_longest as zip_longest # For Python 2 compatibility. except ImportError: from itertools import zip_longest # For Python 3. data = [[1, 2], ('X', 'Y', 'Z')] result = list(zip_longest(*data)) print(result) # Output will be [(1,'X'), (2,'Y'), (None,'Z')] under both Pythons versions with default None filling value. ``` 以上脚本首先尝试从 `itertools` 加载 `izip_longest` (这是 Python 2 中的功能),如果失败则加载 `zip_longest` 。这样就可以保证无论在哪种环境下都能找到对应的方法来实现功能需求.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值