[英]How do you rotate the numbers in an numpy array of shape (n,) or (n,1)?
Say I have a numpy array: 说我有一个numpy数组:
>>> a
array([0,1,2,3,4])
and I want to "rotate" it to get: 我想“旋转”它得到:
>>> b
array([4,0,1,2,3])
What is the best way? 什么是最好的方法?
I have been converting to a deque and back (see below) but is there a better way? 我一直在转换到一个双端队列并且回来(见下文)但是有更好的方法吗?
b = deque(a)
b.rotate(1)
b = np.array(b)
Just use the numpy.roll
function: 只需使用numpy.roll
函数:
a = np.array([0,1,2,3,4])
b = np.roll(a,1)
print(b)
>>> [4 0 1 2 3]
See also this question . 另见这个问题 。
numpy.concatenate([a[-1:], a[:-1]])
>>> array([4, 0, 1, 2, 3])
试试这个吧
b = a[-1:]+a[:-1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.