zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表。具体意思不好用文字来表述,直接看示例:
示例1
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = zip(x, y, z)
print xyz
运行的结果是:
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
示例2
x = [1, 2, 3]
y = [4, 5, 6, 7]
xy = zip(x, y)
print xy
运行结果:
[(1, 4), (2, 5), (3, 6)]
示例3
x = [1, 2, 3]
x = zip(x)
print x
运行结果:
[(1,),(2,),(3,)]
示例4
x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]
xyz = zip(x, y, z)
u = zip(*xyz)
print u
运行的结果是:
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
运行过程中,第一次zip的结果为[(1,4,7),(2,5,8),(3,6,9)],之后第二次zip结果便为[(1,2,3),(4,5,6),(7,8,9)]
示例5
x = [1, 2, 3]
r = zip(* [x] * 3)
print r
运行的结果是:
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
原文地址:http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html