假设我有一个 numpy 数组:
>>> a
array([0,1,2,3,4])
我想“旋转”它以获得:
>>> b
array([4,0,1,2,3])
什么是最好的方法?
我一直在转换为双端队列并返回(见下文)但是有更好的方法吗?
b = deque(a)
b.rotate(1)
b = np.array(b)
最佳答案
只需使用 numpy.roll
功能:
a = np.array([0,1,2,3,4])
b = np.roll(a,1)
print(b)
>>> [4 0 1 2 3]
另见 this question .
关于python - 如何旋转形状为 (n,) 或 (n,1) 的 numpy 数组中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969708/