zip函数接受任意多个(包括0个和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)]
x = [1, 2, 3]y = [4, 5, 6, 7]xy = zip(x, y)print xy#结果:[(1, 4), (2, 5), (3, 6)]
x = [1, 2, 3]x = zip(x)print x#结果:[(1,), (2,), (3,)]
x = zip()print x#结果:[]
x = [1, 2, 3]y = [4, 5, 6]z = [7, 8, 9]xyz = zip(x, y, z)#一般认为这是一个unzip的过程u = zip(*xyz)print u#结果:[(1, 2, 3), (4, 5, 6), (7, 8, 9)]
x = [1, 2, 3]r = zip(* [x] * 3)print r#结果:[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
将[(1,), (2,), (3,)]转换为[1,2,3]:
ret = [('192.168.0.30',), ('192.168.0.57',)]ret1 = zip(*ret)ret2 = list(list(ret1)[0])print(ret2)#结果:['192.168.0.30', '192.168.0.57']