Python3.x:zip()函数

Python3.x:zip()函数

1,概述:

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

2,示例:

 (1).示例1:

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
#注意:Python3.x和Python2.x这里的差别
#Python3.x
xyz = list(zip(x, y, z))
#Python2.x
#xyz = zip(x, y, z)
print(xyz)
#输出结果:[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

 (2).示例2:

x = [1, 2, 3]
y = [4, 5, 6, 7]
xy = list(zip(x, y))
print(xy)
#输出结果:[(1, 4), (2, 5), (3, 6)]

 (3).示例3:

x = [1, 2, 3]
x = list(zip(x))
print(x)
#输出结果:[(1,), (2,), (3,)]

 (4).示例4:

x = list(zip())
print(x)
#输出结果:[]

 (5).示例5:

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = list(zip(x, y, z))
print(xyz )
u = list(zip(*xyz))
print(u)
#输出结果:
#[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
#[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

 注意:Python3.x和Python2.x差别;

 

作者:整合侠
链接:http://www.cnblogs.com/lizm166/p/8232665.html
来源:博客园
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

posted @ 2018-01-07 22:37  整合侠  阅读(2150)  评论(0编辑  收藏  举报