英文文档:

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.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n). This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.

  聚合传入的每个迭代器对象中相同位置的元素,返回一个新的元组类型迭代器

说明:

  1. 函数功能是聚合传入的每个迭代器中相同位置的元素,返回一个新的元组类型迭代器。

>>> x = [1,2,3]
>>> y = [4,5,6]
>>> xy = zip(x,y)
>>> xy #xy的类型是zip类型
<zip object at 0x0429C828>
#导入Iterable
>>> from collections import Iterable
>>> isinstance(xy,Iterable) #判断是否可迭代对象
True
>>> list(xy) #结果
[(1, 4), (2, 5), (3, 6)]

  2. 如果传入的迭代器长度不一致,最短长度的迭代器迭代结束后停止聚合。

>>> x = [1,2,3] #长度3
>>> y = [4,5,6,7,8] #长度5
>>> list(zip(x,y)) # 取最小长度3
[(1, 4), (2, 5), (3, 6)]

  3. 如果只传入一个迭代器,则返回的单个元素元组的迭代器。

>>> list(zip([1,2,3]))
[(1,), (2,), (3,)]

  4. 如果不传入参数,则返回空的迭代器。

>>> list(zip())
[]

  5. zip(*[iter(s)]*n)等效于调用zip(iter(s),iter(s),...,iter(s))。

>>> x = [,,]

>>> list(zip(*[x]*))
[(, , ), (, , ), (, , )] >>> list(zip(x,x,x))
[(, , ), (, , ), (, , )]

  6.反zip。

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = zip(x, y, z)
u = zip(*xyz)
print u

运行的结果是:

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

一般认为这是一个unzip的过程,它的运行机制是这样的:

在运行zip(*xyz)之前,xyz的值是:[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

那么,zip(*xyz) 等价于 zip((1, 4, 7), (2, 5, 8), (3, 6, 9))

所以,运行结果是:[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

注:在函数调用中使用*list/tuple的方式表示将list/tuple分开,作为位置参数传递给对应函数(前提是对应函数支持不定个数的位置参数)

注:python3中已经将zip 后的结果换成了迭代器对象。

Python内置函数(38)——zip的更多相关文章

  1. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

  2. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  3. Python内置函数(67)——zip

    英文文档: zip(*iterables) Make an iterator that aggregates elements from each of the iterables. Returns ...

  4. Python内置函数(38)——list

    英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...

  5. Python 内置函数 -- zip(), sorted(), filter()和map()

    内置函数1. zip() 打包(木桶效应)描述: zip() 函数用于将可迭代的对象作为参数, 将对象中对应的元素打包成一个个元组, 然后返回由这些元组组成的列表语法: zip([iterable, ...

  6. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  7. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  8. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  9. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

随机推荐

  1. c# winform中的一段代码赏析

    我遇到了一个bug,是客户测试我们的产品,报出来的,而且有异常信息文件,这对于定位问题,很有帮助. 我找到源码看了下,bug还无法重现.于是我随便点点客户端,经过了几次调试,结果报出错误来了.客户端界 ...

  2. Error:Execution failed for task ':app:processDebugGoogleServices'. > No matching client found for package name 'com.fortythree.sos.flashlight'

    Q:导入json文件时的包名不对 A:包名存在的位置是app build gradle中的applicationID

  3. Unity 读取资源(图片)

    方法一: 采用Resource.Load方法读取,读取在Unity中Assets下Resources目录下的资源名(不采用后缀). //图片放在Asset/Resources/ Texture2D t ...

  4. 温故而知新--hashtable

    哈希在实际使用中主要是当作私有内存,对数据进行插入和查找,哈希数据元素越多,操作的时候消耗的性能就越到,最明显的是当数据元素达到哈希的容量大小时,插入数据冲突概率会变大,并慢慢的退化为数组. 本例子中 ...

  5. nginx里的sticky的作用

    1.Sticky工作原理 : Sticky是nginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的请求落在同一台服务器上,默认标识 ...

  6. ArrayList 源码分析

    ArrayList 源码分析 1. 结构   首先我们需要对 ArrayList 有一个大致的了解就从结构来看看吧. 1. 继承   该类继承自 AbstractList 这个比较好说 2. 实现 这 ...

  7. 【Python】 子进程创建与使用subprocess

    subprocess *****本文参考了Vamei大神的http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html 运用subproce ...

  8. 设计模式 --> (2)单例模式

    单例模式 单例模式也称为单件模式.单子模式,可能是使用最广泛的设计模式.其意图是保证一个类仅有一个实例,并提供一个访问它的全局访问点,该实例被所有程序模块共享.如系统的日志输出,GUI应用必须是单鼠标 ...

  9. PO BO VO DTO POJO DAO DO

    PO BO DTO VO 归在一起叫是POJO,简单java对象:DAO 是进行数据库增删改查的类,DO不确定有没有. 重点说下POJO PO 持久对象,数据: BO 业务对象,封装对象.复杂对象 , ...

  10. spring MVC 原理及源码解析

    首先要知道springmvc的核心控制器dispatcherServlet(继承自httpServlet) 一般httpServlet的请求过程: 1.初始化(创建servlet实例时)时会执行ser ...