>>> 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()的更多相关文章

  1. python zip文件密码爆破

    #!/usr/bin/env # coding=UTF-8 import zipfile import threading import os import sys class CrackZip: d ...

  2. python zip函数(11)

    一.zip函数描述和使用 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的对象,返回的结果可以直接强转为list列表,这样做的好处是节约了不少的 ...

  3. Python ZIP 文件创建与读取

    Automate the Boring Stuff 学习笔记 02 Python 内置的 zipfile 模块可以对文件(夹)进行ZIP格式的压缩和读取操作.要进行相关操作,首先需要实例化一个 Zip ...

  4. python zip()函数

    描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符 ...

  5. Python zip Python zip函数

    zip([iterable, ...])zip()是Python的一个内建函数,它接受一系列可迭代的对象作为参数,将对象中对应的元素打包成一个个tuple(元组),然后返回由这些tuples组成的li ...

  6. 【转】Python zip() 函数

    转自:http://www.runoob.com/python/python-func-zip.html 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回 ...

  7. Python: zip函数

    zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表. 参考链接解释

  8. Python ZIP压缩

    ru=lambda x:x.decode('u8') rp=lambda x:x.replace('\\','/') gb=lambda x:x.decode('gbk') class ZIP: de ...

  9. python zip enumerate函数

    zip是一个内置函数, 接受两个或多个序列,并将他们拉到一起,成为一个元组列表.每个元组包含各个序列中的一个元素. s = 'abc' t = [0,1,2] zip(s,t) >>> ...

随机推荐

  1. Java应用程序项目的打包与发行(run.bat形式)

    参考: http://www.iteye.com/topic/57312 背景: 以前一直都是在eclipse上面创建应用程序,每次要要运行的时候都要打开eclipse, 直到有个同事叫我帮忙写一个应 ...

  2. js 数组去重

    这是一道常见的面试题,最近在做[搜索历史记录]功能也用到,开始用了 indexOf 方法,该方法在 ECMA5才有支持,对于 IE8- 就不支持了. 我们可以自己写一个函数(Array对象的方法都是定 ...

  3. AngularJS中实现无限级联动菜单(使用demo)

    昨天没来得及贴几个使用demo,今天补上,供有兴趣的同学参考 :) 1. 同步加载子选项demo2. 异步加载子选项demo3. 初始值回填demo4. 倒金字塔依赖demo directive的源代 ...

  4. .NET平台下的微信SDK(Rabbit.WeiXin)开源发布

    在上一篇文章<RabbitHub开源情况及计划>上有提及到了一个新的开源项目——微信SDK,经过几天的努力现在开源发布Beta1版本. 目录 前言 特点 功能 支持的消息类型 请求消息 事 ...

  5. 安装rpm包时遇到Header V3 DSA signature: NOKEY时解决办法

    安装rpm包,特别是没有GPGkey校验,原因是rpm版本过低导致的. 代码如下: warning: rpmts_HdrFromFdno: Header V3 DSA signature: NOKEY ...

  6. jQuery总结(2016-7-4)

    1.事件对应有事件的方法, 如click事件, 有click()方法. 2.jQuery动画 3.AJAX是与服务器交换数据的技术! load()方法     get()方法    post()方法

  7. Hibernate用注解实现实体类和表的映射

    数据库mysql: 1.一对一 person50表password50表是一对一的关系: password50表中有外键 person_id person实体类: package com.c50.en ...

  8. Spring MVC 相关资料整理

    来源于:http://www.cnblogs.com/ylhssn/p/4062757.html 1.概述 Spring MVC是一种基于Java实现MVC设计模式的请求驱动类型的轻量级Web框架,即 ...

  9. 【BZOJ 3150】新Nim游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3105 并不会QwQ 为什么贪心是正确的. 向小神请教了一个弱智问题(小神好神啊OTZ) 然后就写了一 ...

  10. 【线性规划与网络流 24题】已完成(3道题因为某些奇怪的原因被抛弃了QAQ)

    写在前面:SDOI2016 Round1滚粗后蒟蒻开始做网络流来自我拯救(2016-04-11再过几天就要考先修课,现在做网络流24题貌似没什么用←退役节奏) 做的题目将附上日期,见证我龟速刷题. 1 ...