zip有拉链的意思,zip函数像拉链一样将0个或多个可迭代对象按相同位置组合成一个zip对象,该zip对象的每个元素是由每个可迭代对象的相同位置的元素组成的元祖。

如果zip中有多个序列,而各序列的长度不同,那么返回的对象的长度以最短为准,超出的部分不返回。

如果zip中只有一个序列,则返回对象的每个元祖中只有一个元素。如果zip没有给参数,那么返回一个空对象。

用法:

zip(*iterables)
Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.
>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [7,8,9,10] >>> zip(a,b)
<zip object at 0x00000000027D2FC8>
>>> z=zip(a,b)
>>> print(z)
<zip object at 0x00000000027D6088> >>> for i in z:
... print(i)
...
(1, 4)
(2, 5)
(3, 6)
>>> x=zip(a)
>>> for i in x:
... print(i)
...
(1,)
(2,)
(3,)
>>> y=zip(a,b,c)
>>> for i in y:
... print(i)
...
(1, 4, 7)
(2, 5, 8)
(3, 6, 9)

另外zip函数可以使用星号解开zip对象,即unzip。

zip() in conjunction with the * operator can be used to unzip a list.
>>> m=zip(*zip(a,b,c))
>>> for i in m:
... print(i)
...
(1, 2, 3)
(4, 5, 6)
(7, 8, 9)

参考:https://docs.python.org/3/library/functions.html#zip

最新文章

  1. QQ右下角图标不见了
  2. go sample - hello world
  3. STL_关联容器 VS C++ hashmap
  4. JavaWeb chapter 4 Servlet处理HTTP请求
  5. Html之初体验
  6. 【C#】窗体动画效果
  7. TOM大叔的几道Javascript题目与解答
  8. PHP错误处理及异常处理笔记
  9. 在Java中怎样把数组转换为ArrayList?
  10. 一个简单的PHP登录演示(SESSION版 与 COOKIE版)
  11. Gradle构建Java Web应用(转)
  12. memset memcpy函数
  13. [CodeForces 11D] A Simple Task - 状态压缩入门
  14. 有效防止softmax计算时上溢出(overflow)和下溢出(underflow)的方法
  15. 面试官:你分析过SpringMVC的源码吗?
  16. ElasticSearch(五) Elasticsearch-jdbc实现MySQL同步到ElasticSearch
  17. Django中的forms一些小点
  18. 上周日选拔题部分write up
  19. [smarty] 在smarty模板中使用smarty变量初始化 javascript 变量的问题
  20. 轻量ORM-SqlRepoEx (十四)最佳实践之Dapper(1)

热门文章

  1. kettle学习笔记(十)——数据检验、统计、分区与JS脚本
  2. 用pyinstaller把python代码打包成exe可执行文件
  3. 20155202张旭 Exp4 恶意代码分析
  4. WPF编程,C#中对话框自动关闭的一种方法。
  5. 第五节 HTML&amp;CSS -- 关于浮动和清除浮动的解说,以及两个大坑不要踩
  6. 显示 隐藏DIV的技巧
  7. mpvue两小时,产出一个《点钞辅助工具》小程序
  8. 【Android UI设计与开发】第04期:引导界面(四)仿人人网V5.9.2最新版引导界面
  9. Ajax引擎:ajax请求步骤详细代码
  10. Golang Context 详细介绍