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.
就是把多个序列或者是迭代器的元素,组合成元组。返回的元组的长度是所有输入序列中最短的
组合之后的元组长度是依照两个输入序列中最短的a为准的。
如果输入的两个序列都是特别大的情况,zip就会很慢了。使用izip比较下。
这样看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来制定那些缺失值的默认值
探究一下,基本的区别就是这些,具体的使用要看具体的编程场景。
原文链接:http://blog.youkuaiyun.com/orangleliu/article/details/23737701?utm_source=tuicool&utm_medium=referral