Python内置函数(38)——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.
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的更多相关文章
- Python之路Python内置函数、zip()、max()、min()
Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...
- Python之路(第八篇)Python内置函数、zip()、max()、min()
一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...
- Python内置函数(67)——zip
英文文档: zip(*iterables) Make an iterator that aggregates elements from each of the iterables. Returns ...
- Python内置函数(38)——list
英文文档: class list([iterable]) Rather than being a function, list is actually a mutable sequence type, ...
- Python 内置函数 -- zip(), sorted(), filter()和map()
内置函数1. zip() 打包(木桶效应)描述: zip() 函数用于将可迭代的对象作为参数, 将对象中对应的元素打包成一个个元组, 然后返回由这些元组组成的列表语法: zip([iterable, ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python内置函数
python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
随机推荐
- 微信小程序-weui实例代码提取
搜索框 对应代码如下: wxss文件 <view class="page"> <view class="page__hd"> <v ...
- 什么是IPFS?(二)
IPFS的的"宏伟"目标是取代HTTP, 那么先来看看IPFS是如何工作的? IPFS为每一个文件分配一个独一无二的哈希值(文件指纹: 根据文件的内容进行创建), 即使是两个文件内 ...
- python作业02
1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['alex', 'eric', 'rain'] li = ['alex', 'eric', 'rain'] v = "_& ...
- springMVC正确使用GET POST PUT和DELETE方法,如何传递参数
1. 向服务器请求数据:GET 这是标准的http的GET最擅长的, 应该使用GET请求,但是在使用时候我们会需要传递一个或多个参数给服务器, 这些出参数可能是基本数据类型页可能是对象,get方法可以 ...
- 笔记:MyBatis 日志显示-log4j2
在ClassPath路径创建log4j2.xml配置文件,增加如下日志配置: <?xml version="1.0" encoding="UTF-8"?& ...
- java中StringUtils中isEmpty 和isBlank的区别
StringUtils在commons-lang-2.2.jar包中:org.apache.commons.lang.StringUtils ; StringUtils方法的操作对象是java.lan ...
- 跨域的另一种解决方案CORS(CrossOrigin Resource Sharing)跨域资源共享
在我们日常的项目开发时使用AJAX,传统的Ajax请求只能获取在同一个域名下面的资源,但是HTML5打破了这个限制,允许Ajax发起跨域的请求.浏览器是可以发起跨域请求的,比如你可以外链一个外域的图片 ...
- Jmeter4.0----安装教程(2)
1.检查安装环境 1.1 JDK要求 JDK版本:1.6 + 1.2 检查是否安装JDK win + R 快捷键打开运行,输入 cmd 打开面板,在面板中输入 java -version,出现如下信息 ...
- 【CSS】 CSS的一些应用实例和参考
css 一些应用实例 基本抄自http://www.w3school.com.cn/css/css_align.asp ..把这些知识消化吸收然后以自己的话来解释一下 ■ 对齐 ● 用margin属 ...
- lvs(dr)+keepalived
系统:centos6.5mini 环境: 机器名 Ip地址 角色 Vip-web: 192.168.20.50 Vip-mysql: 192.168.20.60 lvs01 192.168.20.10 ...