[Python]zip 和 izip , izip_longest比较,,本文链接:https


本文链接:https://blog.csdn.net/lzz957748332/article/details/23737701

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    %timeitIn [32]: %timeit(zip(a,b))1 loops, best of 3: 811 ms per loopIn [33]: import itertoolsIn [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 和 izip , izip_longest比较

评论关闭