【问题标题】:What's the numpy equivalent of python's zip(*)?python的zip(*)的numpy等价物是什么?
【发布时间】:2023-03-20 20:55:01
【问题描述】:

我认为(希望)这个问题与What is the equivalent of "zip()" in Python's numpy? 有很大不同,尽管这可能只是我的无知。

假设我有以下内容:

[[[ 1, 2], [3, 4], [5, 6]], [[ 7, 8], [9, 10], [11, 12]]]

我想把它变成

[[[ 1, 2], [7, 8]], [[ 3, 4], [9, 10]], [[ 5, 6], [11, 12]]]

在python中我可以做到:

>>> foo
[[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]
>>> zip(*foo)
[([1, 2], [7, 8]), ([3, 4], [9, 10]), ([5, 6], [11, 12])]

但是如何使用 numpy 数组(不使用 zip(*))来做到这一点?

【问题讨论】:

    标签: python numpy zip


    【解决方案1】:

    您真的需要返回元组还是要重塑数组?

    >>> a
    array([[[ 1,  2],
            [ 3,  4],
            [ 5,  6]],
    
           [[ 7,  8],
            [ 9, 10],
            [11, 12]]])
    
    >>> a.swapaxes(0,1)
    array([[[ 1,  2],
            [ 7,  8]],
    
           [[ 3,  4],
            [ 9, 10]],
    
           [[ 5,  6],
            [11, 12]]])
    

    【讨论】:

    • 谢谢!正是我想要的。
    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2019-03-24
    • 2011-09-03
    • 2011-03-07
    • 2016-09-16
    • 2013-10-18
    相关资源
    最近更新 更多