Python内置函数(67)——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 = [1,2,3] >>> list(zip(*[x]*3))
[(1, 1, 1), (2, 2, 2), (3, 3, 3)] >>> list(zip(x,x,x))
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
Python内置函数(67)——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内置函数(38)——zip
英文文档: zip(*iterables) Make an iterator that aggregates elements from each of the iterables. Returns ...
- Python 内置函数 -- zip(), sorted(), filter()和map()
内置函数1. zip() 打包(木桶效应)描述: zip() 函数用于将可迭代的对象作为参数, 将对象中对应的元素打包成一个个元组, 然后返回由这些元组组成的列表语法: zip([iterable, ...
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
- 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 ...
- Python基础篇【第2篇】: Python内置函数(一)
Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位 ...
随机推荐
- windows 下 nginx log 分割
默认 nginx 不支持 log自动分割 windows下 解决方案: 1.首先创建bat脚本 split_log.bat , 并保存在nginx 目录下: @echo off rem ...
- swagger结合dubbo的rest服务测试
swagger结合dubbo的rest服务测试 背景介绍 我们应用的dubbo服务导出,可能没有直接的触发点去发起调用测试,除非自己手写controller和test类,缺乏一个动态工具,类似流行的s ...
- shiro登录密码加密
密码加密 String passwd = new SimpleHash("SHA-1", "username", "password").t ...
- BP神经网络综合评价法
BP神经网络综合评价法是一种交互式的评价方法,一种既能避免人为计取权重的不精确性, 又能避免相关系数求解的复杂性,还能对数量较大且指标更多的实例进行综合评价的方法,它可以根据用户期望的输出不断修改指标 ...
- ELK logstash geoip值为空故障排查
首先我们用的是elasticsearch+kibana+logstash+filebeat 客户端filebeat收集日志后经过服务端logstash规则处理后储存到elasticsearch中,在k ...
- 基于Emgucv,C#的图片旋转方式
/// <summary> /// 图片旋转 --百度 旋转仿射 /// </summary> /// <param name="modelImage" ...
- java位移运算符2 转
https://blog.csdn.net/xxx134617/article/details/7454774 java中int类型占4个字节,二进制用补码表示: 3的二进制表示: 00000000 ...
- HTTP协议头部与Keep-Alive模式详解(转)
转自:http://a280606790.iteye.com/blog/1095085 http1.1 中怎么打开持久连接,怎么关闭,怎么传输数据(确定本次数据是否传输完毕) 1.什么是Keep-Al ...
- MySQL基于左右值编码的树形数据库表结构设计
MySQL基于左右值编码的树形数据库表结构设计 在关系型数据库中设计树形的数据结构一直是一个十分考验开发者能力的,最常用的方案有主从表方案和继承关系(parent_id)方案.主从表方案的最大缺点 ...
- Tomcat手动部署Web项目详细步骤
阅读须知:文章基于Tomcat8,其它版本若有差异,请自行辨别.本文为博主原创文章,转载请附原文链接. 不借助任何IDE,这里介绍在Tomcat中手动部署web项目的三种方式: 1.部署解包的weba ...