该示例将函数zip应用于两个列表是这样的:
x = [1, 2, 3] y = [4, 5, 6] zipped = zip(x, y) #show list(zipped) [(1, 4), (2, 5), (3, 6)]
但是现在,如果我喜欢:
array = [ [1,2,3], [3,4,5] , [6,7,8] ... ]
如何应用功能zip显示如下:
[(1,3,6,...),(2,4,7,...),(3,5,8,...),... (....) ]
您需要通过“ splat”或“ star”运算符解压缩参数:
zip(*array)
例:
>>> array = [ [1,2,3], [3,4,5] , [6,7,8] ] >>> print ( list(zip(*array)) ) [(1, 3, 6), (2, 4, 7), (3, 5, 8)]