如何在numpy数组形状(n,)或(n,1)中旋转数字?

ato*_*3ls 2 python numpy

说我有一个numpy数组:

>>> a 
array([0,1,2,3,4])
Run Code Online (Sandbox Code Playgroud)

我想"旋转"它得到:

>>> b
array([4,0,1,2,3])
Run Code Online (Sandbox Code Playgroud)

什么是最好的方法?

我一直在转换到一个双端队列并且回来(见下文)但是有更好的方法吗?

b = deque(a)
b.rotate(1)
b = np.array(b)
Run Code Online (Sandbox Code Playgroud)

Fra*_*ano 9

只需使用该numpy.roll功能:

a = np.array([0,1,2,3,4])
b = np.roll(a,1)
print(b)
>>> [4 0 1 2 3]
Run Code Online (Sandbox Code Playgroud)

另见这个问题.