【问题标题】:How to apply function zip to n-list [duplicate]如何将函数 zip 应用于 n-list [重复]
【发布时间】:2013-01-11 23:14:31
【问题描述】:

将函数 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,...),... (....) ]

【问题讨论】:

    标签: python arrays list


    【解决方案1】:

    您需要argument unpacking 通过“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)]
    

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 2021-11-30
      • 2011-11-13
      • 1970-01-01
      相关资源
      最近更新 更多