python zip()
>>> help(zip)
Help on built-in function zip in module __builtin__: zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)] Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence. >>> list_1 = ['name', 'age']
>>> list_2 = ['wang', 23]
>>> zip(list_1, list_2)
[('name', 'wang'), ('age', 23)]
>>> dict(zip(list_1, list_2))
{'age': 23, 'name': 'wang'}
如果两个参数不一样长,那么取短的。
也可以反向操作,见下面:
>>> list_3
{'age': 23, 'name': 'wang'}
>>> list_1 = ['name', 'age']
>>> list_2 = ['wang', 23]
>>> list_3 = zip(list_1, list_2)
>>> list_3
[('name', 'wang'), ('age', 23)]
>>> l1, l2 = zip(*list_3)
>>> list_1 == list(l1)
True
>>> type(l1)
<type 'tuple'>
>>> list_2 == l2
False
>>> list_2 == list(l2)
True
自然,也可以操作三个或者一个参数:
>>> zip(list_1)
[('name',), ('age',)]
>>> zip(list_1, list_2, l1)
[('name', 'wang', 'name'), ('age', 23, 'age')]
python.org的解释:
1. This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.
2. 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).
3.zip() in conjunction with the * operator can be used to unzip a list
python zip()的更多相关文章
- python zip文件密码爆破
#!/usr/bin/env # coding=UTF-8 import zipfile import threading import os import sys class CrackZip: d ...
- python zip函数(11)
一.zip函数描述和使用 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,返回的结果可以直接强转为list列表,这样做的好处是节约了不少的 ...
- Python ZIP 文件创建与读取
Automate the Boring Stuff 学习笔记 02 Python 内置的 zipfile 模块可以对文件(夹)进行ZIP格式的压缩和读取操作.要进行相关操作,首先需要实例化一个 Zip ...
- python zip()函数
描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符 ...
- Python zip Python zip函数
zip([iterable, ...])zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的li ...
- 【转】Python zip() 函数
转自:http://www.runoob.com/python/python-func-zip.html 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回 ...
- Python: zip函数
zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表. 参考链接解释
- Python ZIP压缩
ru=lambda x:x.decode('u8') rp=lambda x:x.replace('\\','/') gb=lambda x:x.decode('gbk') class ZIP: de ...
- python zip enumerate函数
zip是一个内置函数, 接受两个或多个序列,并将他们拉到一起,成为一个元组列表.每个元组包含各个序列中的一个元素. s = 'abc' t = [0,1,2] zip(s,t) >>> ...
随机推荐
- canvas drag 实现拖拽拼图小游戏
博主一直心心念念想做一个小游戏- 前端时间终于做了一个小游戏,直到现在才来总结,哈哈- 以后要勤奋点更新博客! 实现原理 1.如何切图? 用之前的方法就是使用photoshop将图片切成相应大小的图 ...
- CUDA2.1-原理之索引与warp
本小节来自<大规模并行处理器编程实战>第四节,该书是很好的从内部原理结构上来讲述了CUDA的,对于理解CUDA很有帮助,借以博客的形式去繁取间,肯定会加入自己个人理解,所以有错误之处还望指 ...
- 高性能JavaScript DOM编程
我们知道,DOM是用于操作XML和HTML文档的应用程序接口,用脚本进行DOM操作的代价很昂贵.有个贴切的比喻,把DOM和JavaScript(这里指ECMScript)各自想象为一个岛屿,它们之间用 ...
- .net程序员转行做手游开发经历(二)
上篇主要介绍自己个人的经历,这篇主要讲下学习新语言的过程. 上次说到最终选择的语言是swift,框架用spritekit,上次有网友对为什么选择用这俩呢,为什么不用cocos和unity呢,cocos ...
- 正则表达式语法(msdn)
“正则表达式”描述在搜索文本正文时要匹配的一个或多个字符串.该表达式可用作一个将字符模式与要搜索的字符串相匹配的模板. 正则表达式包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为“元字符” ...
- Html.BeginForm
该方法用于构建一个From表单的开始,他的构造方法为: Html.BeginForm("ActionName","ControllerName",FormMet ...
- 作业4-两人编程<词频统计>
协作:苗中峰,刘鑫成 我主要攻克排序,成哥写了文件流的使用.整合工作由我完成,成哥帮我查阅资料,避免和解决语法错误. 这次任务较作业三的变化是: * ...
- 【Alpha版本】冲刺阶段——Day 4
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...
- Beta版本冲刺———第五天
会议照片: 项目燃尽图: 1.项目进展: 困难:基本计划中增加的功能已经完成,但是在"如何保存每次游戏的分数,并将其排序列在排行榜中"遇到麻烦,现在小组都在一起协商攻克中.
- 在编译php事务时候出现如下错误,具体原因不知,不过解决了
在make的时候出现如下错误 libtool: link: `ext/date/lib/parse_date.lo' is not a valid libtool objectmake: *** [l ...