将函数zip应用于两个alist的示例是:
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,...),... (....) ]
答案 0 :(得分:5)
您需要通过“splat”或“star”运算符argument unpacking:
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)]