如何在 python 中移动二维 numpy 数组?

How to shift a 2D numpy array in python?

如何在 Python 中移动二维 numpy 数组的元素?

例如转换这个数组:

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

到这个数组:

[[0, 0, 0,],
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9]]

我想手动做,切片是这样的:

import numpy as np

A = np.array([[ 1, 2, 3],
              [ 4, 5, 6],
              [ 7, 8, 9],
              [10, 11, 12]])

A[1:,:] = A[:3,:]
A[:1,:] = [0, 0, 0]
import numpy as np
a = np.array([[ 1, 2, 3],
              [ 4, 5, 6],
              [ 7, 8, 9],
              [10, 11, 12]])    

def shift_array(array, place):
    new_arr = np.roll(array, place, axis=0)
    new_arr[:place] = np.zeros((new_arr[:place].shape))
    return new_arr

shift_array(a,2)

# array([[ 0,  0,  0],
#        [ 0,  0,  0],
#        [ 1,  2,  3],
#        [ 4,  5, 6]])