zoukankan      html  css  js  c++  java
  • [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)


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

  • 相关阅读:
    菜单按钮及导航
    实现点击箭头切换图片页和相册滚动
    网页设计的基本原则
    网格系统
    表单系列2
    类与对象学习总结
    汉诺塔的最简的步骤思路
    3.31作业解答
    初学java 用if语句做几个小程序
    做三个java初期学习的练习Var1~3为头目标
  • 原文地址:https://www.cnblogs.com/feifeifeisir/p/13689874.html
Copyright © 2011-2022 走看看