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关键字.开发者可以在任何可以使用函数引用的位 ...
随机推荐
- DDD - 概述 - (一)
本片将介绍以下内容: 1).DDD是什么? 2).怎么使用DDD? 3).使用DDD应该规避或者注意什么? 一.DDD是什么? 简言之:领域驱动设计(domain driven design),顾名思 ...
- (转载)Linux终端复用神器-Tmux使用
Linux终端复用神器-Tmux使用 转载地址:https://blog.51cto.com/652465/2094738 Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于Open ...
- python 在ubuntu下安装pycurl
https://www.linuxidc.com/Linux/2016-05/131574.htm
- Java 平时作业四
编写一个Java程序实现返回指定目录及其子目录下扩展名为*.pdf的所有文件名. 扩展: isFile public boolean isFile() 测试此抽象路径名表示的文件是否为普通文件. 如果 ...
- python可视化库 Matplotlib 00 画制简单图像
1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...
- Python函数参数&time、OS、json模块
##可变参数 PORT = 3306 #常量 def mysql(host,user,password,port,charset,sql,db): print('连接mysql') # mysql(' ...
- django -使用jinja2模板引擎 自定义的过滤器
setting.py中 TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [os.path.jo ...
- kubernates使用kubeadm安装
kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,伴随Kubernetes每个版本的发布都会同步更新,kubeadm会对集群配置方面的一些实践做调整,通过实验k ...
- centos docker 升级至最新稳定版--摘自官网
亲测好使 删除老版本的docker sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ do ...
- git教程——安装配置
Git(读音为/gɪt/.)是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个 ...