>>> 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

最新文章

  1. iOS开发——高级特性&amp;Runtime运行时特性详解
  2. 有米实习-用到的shell脚本和Python脚本记录
  3. Elasticsearch1.7到2.3升级实践总结
  4. maven学习讲解
  5. 转:使用vs2013打开VS2015的工程文件的解决方案(适用于大多数vs低版本打开高版本)
  6. IBatisNet基础组件
  7. Machine Learning in Action -- 回归
  8. 用Js的eval解析JSON中的注意点
  9. css position 绝对定位和相对定位
  10. Searching in a Radius using Postgres[Marked]
  11. Visual Studio Code使用typings拓展自动补全功能
  12. Vue(三十三)国际化解决方案
  13. 滑动窗口最大值的golang实现
  14. Hadoop学习笔记(三):java操作Hadoop
  15. 非root用户sudo_ssh免密钥
  16. Navicat for MySQL连接mysql数据库时提示错误:Can&#39;t connect to MySQL server (10060)
  17. Maven依赖中的scope详解
  18. cdh 安装步骤
  19. February 2 2017 Week 5 Thursday
  20. angular1结合webpack构建工具

热门文章

  1. 如何在 apache 中设置缓存有效时间
  2. 谈谈数据监听observable的实现
  3. Python时间性能测量
  4. md5加密篇(一)
  5. RabbitMQ官方中文入门教程(PHP版) 第四部分:路由(Routing)
  6. there is issue about change event of checkbox in the ie8 oe ie7
  7. Thinkphp url 除去index.php
  8. viewSub惰性装载器
  9. zoj3261 并查集离线处理
  10. bzoj1012