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差别;
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/16791.html