Python zip函数
2015-08-29 21:11
741 查看
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.
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).
zip() in conjunction with the * operator can be used to unzip a list:
pythonAPI上面说的
下面自己上例子:
结果是:
可以看出zip是把同一维度的数据放在一个list中
结果是:
for是把list中的tuple中的每个元素输出
结果:
这是zip(*w)是把功能返回去而已
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).
zip() in conjunction with the * operator can be used to unzip a list:
pythonAPI上面说的
下面自己上例子:
x=[1,2,3] y=[4,5,6] z=[7,8,9] print zip(x) print zip(x,y) print zip(x,y,z)
结果是:
[(1,), (2,), (3,)] [(1, 4), (2, 5), (3, 6)] [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
可以看出zip是把同一维度的数据放在一个list中
w= zip(x,y,z) for a,b,c in w: print 'a:{0},b:{1},c:{2}'.format(a,b,c)
结果是:
a:1,b:4,c:7 a:2,b:5,c:8 a:3,b:6,c:9
for是把list中的tuple中的每个元素输出
print type(w), type(w[1])
<type 'list'> <type 'tuple'>
x=[1,2,3] y=[4,5,6] z=[7,8,9] w= zip(x,y,z) print w print zip(*w)
结果:
[(1, 4, 7), (2, 5, 8), (3, 6, 9)] [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
这是zip(*w)是把功能返回去而已
相关文章推荐
- python_学习笔记0829
- python __base__等内置方法
- python中get、post数据
- Windows搭建python开发环境,python入门到精通[一]
- 一步步学习Python-django开发-Mac下搭建Python-Django环境
- Python中的列表生成式和多层表达式
- 使用Python脚本将文字转换为图片的实例分享
- 利用python收发邮件功能实现远程电脑的控制
- Python 函数之参数、局部变量
- Python扫描内网存活的主机
- python2.7 beautifulsoup学习使用
- python2.7 beautifulsoup安装下载
- Python分词模块推荐:jieba中文分词
- python内置函数汇总(2)
- python类型转换、数值操作
- Python 函数之函数定义、调用、传参
- Python找回MySQL密码
- Python之包管理工具
- Python中的编码问题
- 理解Python中的with…as…语法