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.

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

[python]  view plain copy print ?
  1. In [27]: a = ['a', 'b', 'c', 'd', 'e']
  2. In [28]: b = range(10)
  3. In [29]: zip(a,b)
  4. Out[29]: [('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)]

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

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

[python]  view plain copy print ?
  1. In [30]: a = range(10000000)
  2. In [31]: b = range(10000000)
  3. In [32]: tim
  4. %%timeit %time    %timeit
  5. In [32]: %timeit(zip(a,b))
  6. 1 loops, best of 3: 811 ms per loop
  7. In [33]: import itertools
  8. In [34]: %timeit(itertools.izip(a,b))
  9. 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来制定那些缺失值的默认值

[python]  view plain copy print ?
  1. In [35]: a = ['a','b','c']
  2. In [36]: b = range(10)
  3. In [37]: itertools.izip_longest(a,b,fillvalue=-1)
  4. Out[37]: <itertools.izip_longest at 0x250e540>
  5. In [38]: c = itertools.izip_longest(a,b,fillvalue=-1)
  6. In [42]: for i in c:
  7. ....:     print i
  8. ....:
  9. ('a', 0)
  10. ('b', 1)
  11. ('c', 2)
  12. (-1, 3)
  13. (-1, 4)
  14. (-1, 5)
  15. (-1, 6)
  16. (-1, 7)
  17. (-1, 8)
  18. (-1, 9)

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

原文链接:http://blog.csdn.net/orangleliu/article/details/23737701?utm_source=tuicool&utm_medium=referral

python中zip 和 izip , izip_longest比较相关推荐

  1. python中zip什么意思_浅谈Python中的zip()与*zip()函数详解 python的zip函数加上一个*号,是什么含义...

    python 当中的zip( )函数到底是干嘛的?你越来越善解人意,就没人在意你的委屈和脾气. zip([1,2,3],['a','b','c']) 结果是 [(1, 'a'), (2, 'b'), ...

  2. Python中zip()、zip(*zipped)、*zip()函数总结

    前言:本博文主要讲解Python中zip().zip(*zipped).*zip()的用法及区别. 文章目录 一.zip()函数 1.1.语法 1.2.返回值 1.3.实例 二.zip(*zipped ...

  3. Python中zip()、zip(*zipped)、*zip()的用法及区别

    Python中zip().zip(*zipped).*zip()的用法及区别

  4. python中zip()函数的用法_Python zip()函数用法实例分析

    本文实例讲述了python zip()函数用法.分享给大家供大家参考,具体如下: 这里介绍python中zip()函数的使用: >>> help(zip) Help on built ...

  5. Python中zip()函数的解释和可视化

    文章来源于机器学习算法与Python实战,作者爱学习的胡同学 zip()的作用 先看一下语法: zip(iter1 [,iter2 [...]]) -> zip object Python的内置 ...

  6. python中zip的使用_浅谈Python中的zip()与*zip()函数详解

    前言 1.实验环境: Python 3.6: 2.示例代码地址:下载示例: 3.本文中元素是指列表.元组.字典等集合类数据类型中的下一级项目(可能是单个元素或嵌套列表). zip(*iterables ...

  7. python中zip函数的使用方法

    Python 中的 zip 函数可以将多个可迭代对象中的元素打包成一个个元组,然后返回一个可迭代的 zip 对象.比如: # 两个可迭代对象 a = [1, 2, 3] b = [4, 5, 6]# ...

  8. ZH奶酪:Python中zip函数的使用方法

    定义:zip([iterable, -]) zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些 tuples组成 ...

  9. python中zip函数详解_Python中zip函数用法

    看openstack的代码,遇到一个内建函数zip不明白其意思: # Adjust the weights in the grid by the functions weight adjustment ...

最新文章

  1. 数据蒋堂 | 从一道招聘考题谈起
  2. [云炬创业基础笔记]第二章创业者测试23
  3. tinyxml在linux和windows下的编译及使用详解
  4. php 旋转图片 并保存,如何在PHP中旋转并保存图像
  5. Verilog中wire与reg类型的区别(转载自http://www.cnblogs.com/farbeyond/p/5204586.html)
  6. java中goto用法源代码,java中goto语句解析12
  7. C++并发中的条件变量 std::condition_variable
  8. assertion: 18 { code: 18, ok: 0.0, errmsg: auth fails }
  9. C# 输入选择文件夹
  10. 快速了解Linux ps命令
  11. FileNet 开发资料 官方红皮书
  12. xp系统一直跳出宽带连接服务器,xp系统一直显示正在获取网络地址的操作方案...
  13. 使用PS排版制作一寸照片
  14. EIDROS3.9学习(一)
  15. ads的designguide打不开报错
  16. 浅谈一个新人的大数据之路-HiveQLSpark-SQL中谓词下推
  17. 汉语字典APP开发总结
  18. 该换壁纸啦,记录一个用CSS和HTML做的3D立体相册
  19. 计算机一级扫描件,学院年终科研成果统计提交成果扫描件要求
  20. 逍遥情缘服务器维护没通告,【维护公告】2月2日中午12:00维护公告

热门文章

  1. 你曾见过的数据获取,秒速5厘米?
  2. 计算机科学与技术考研北京工业大学分,22考研——北京工业大学电子信息专业考研考情分析...
  3. 计算机检测与维修课程设计总结,(计算机课程设计总结报告.ppt
  4. 公司倒闭,39岁高级程序员再找工作月薪不到八千
  5. VC++2005相关问题解决方案
  6. 中国与西欧的历史发展为何不同
  7. 轻轻松松理解Base64
  8. 《Mesos: A Platform for Fine-Grained Resource Sharing in the Data Center》 论文解读
  9. 慢性咽炎偏方(收集)
  10. 基于微信旅游景区购票小程序毕业设计毕设作品(7)中期检查报告