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) >>> ...
随机推荐
- 矩阵求逆算法及程序实现(C++)
在做课题时,遇到了求多项式问题,利用了求逆方法.矩阵求逆一般使用简单的算法,还有快速算法 如全选主元高斯-约旦消元法,但本文程序主要写了简单的矩阵求逆算法定义法之伴随矩阵求逆公式如下,其中A可逆: , ...
- 爱春秋之戏说春秋 Writeup
爱春秋之戏说春秋 Writeup 第一关 图穷匕见 这一关关键是给了一个图片,将图片下载到本地后,打开以及查看属性均无任何发现,尝试把图片转换为.txt格式.在文本的最后发现这样一串有规律的代码: 形 ...
- 两个Canvas小游戏
或许连小游戏都算不上,可以叫做mini游戏. 没有任何框架或者稍微有点深度的东西,所以有js基础的或者要追求炫酷效果的可以直接ctrl+w了. 先贴出两个游戏的试玩地址: 是男人就走30步 是男人就忍 ...
- JavaScript数独求解器
<html> <head> <style type="text/css"> .txt { width: 50; height: 50; back ...
- 【jQuery EasyUI系列】创建CRUD数据网格
在上一篇中我们使用对话框组件创建了CRUD应用创建和编辑用户信息.本篇我们来创建一个CRUD数据网格DataGrid 步骤1,在HTML标签中定义数据网格(DataGrid) <table id ...
- js函数声明
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- python 登陆接口
#!/usr/bin/env pythonimport sysname = ''pw=''name_num = 0pw_num = 0#black_list = []with open('a.txt' ...
- 【POJ 1113】Wall
http://poj.org/problem?id=1113 夏令营讲课时的求凸包例题,据说是PKUSC2015的一道题 我WA两次错在四舍五入上了(=゚ω゚)ノ #include<cmath& ...
- 平行四边形面积 light 1305
double 不一定是与x y轴平平行 所以要正弦定理和余弦定理 似乎一定要printf输出 错了好几次 #include<iostream> #include<math.h> ...
- 事件冒泡是什么如何用jquery阻止事件冒泡
(1)什么是事件起泡 首先你要明白一点,当一个事件发生的时候,该事件总是有一个事件源,即引发这个事件的对象,一个事件不能凭空产生,这就是事件的发生. 当事件发生后,这个事件就要开始传播.为什么要传播呢 ...