zip函數接受任意多個(包括0個和1個)序列做爲參數,返回一個tuple列表;html
(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:spa
x = [1, 2, 3] y = [4, 5, 6, 7] xy = list(zip(x, y)) print(xy) #輸出結果:[(1, 4), (2, 5), (3, 6)]
(3).示例3:code
x = [1, 2, 3] x = list(zip(x)) print(x) #輸出結果:[(1,), (2,), (3,)]
(4).示例4:htm
x = list(zip()) print(x) #輸出結果:[]
(5).示例5:blog
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差異;ip
做者:整合俠
連接:http://www.cnblogs.com/lizm166/p/8232665.html
來源:博客園
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。get