【问题标题】:Snake traversal of 2D NumPy array2D NumPy 数组的蛇形遍历
【发布时间】:2019-09-04 17:20:18
【问题描述】:

我有以下二维数组:

In [173]: arr
Out[173]: 
array([[ 1,  2,  3,  4],   # -> -> -> ->
       [ 5,  6,  7,  8],   # <- <- <- <-
       [ 9, 10, 11, 12],   # -> -> -> ->
       [13, 14, 15, 16],   # <- <- <- <-
       [17, 18, 19, 20]])  # -> -> -> ->

我想在 snake-like pattern 中遍历数组,从左上角元素开始,到右下角元素结束。

到目前为止,我有这种无趣的解决方法:

In [187]: np.hstack((arr[0], arr[1][::-1], arr[2], arr[3][::-1], arr[4]))
Out[187]: 
array([ 1,  2,  3,  4,  8,  7,  6,  5,  9, 10, 11, 12, 16, 15, 14, 13, 17,
       18, 19, 20])

我们怎样才能以最小的努力做到这一点,没有循环,也没有太多的硬编码?

【问题讨论】:

    标签: python numpy multidimensional-array traversal numpy-ndarray


    【解决方案1】:

    一种方法是从输入的副本开始,然后用输入中相应行的列翻转版本替换第二行,并使用步长切片对所有偶数行执行此操作。最后,最后需要一个ravel() 来获得所需的扁平化版本。

    因此,实现看起来像这样 -

    out = arr.copy()
    out[1::2] = arr[1::2,::-1]
    out = out.ravel()
    

    另一种紧凑的方式是使用 np.where 在 col-flipped 和 non-flipped 版本之间进行选择,从而实现我们想要的输出 -

    np.where(np.arange(len(arr))[:,None]%2,arr[:,::-1],arr).ravel()
    

    用给定样本的解释-

    # Array to be used for the chosing. 1s would be True ones and 0s are False
    In [72]: np.arange(len(arr))[:,None]%2
    Out[72]: 
    array([[0],
           [1],
           [0],
           [1],
           [0]])
    
    # Use np.where to choose. So, arr[:,::-1] must be the first data, as
    # that's one to be put on even rows and arr would be the second one.
    In [73]: np.where(np.arange(len(arr))[:,None]%2,arr[:,::-1],arr)
    Out[73]: 
    array([[ 1,  2,  3,  4],
           [ 8,  7,  6,  5],
           [ 9, 10, 11, 12],
           [16, 15, 14, 13],
           [17, 18, 19, 20]])
    
    # Finally flatten
    In [74]: np.where(np.arange(len(arr))[:,None]%2,arr[:,::-1],arr).ravel()
    Out[74]: 
    array([ 1,  2,  3,  4,  8,  7,  6,  5,  9, 10, 11, 12, 16, 15, 14, 13, 17,
           18, 19, 20])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 2012-04-26
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多