Say I have a numpy array:
假设我有一个numpy数组:
>>> a array([0,1,2,3,4])
and I want to "rotate" it to get:
我要旋转它得到
>>> barray([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?
我已经转换成deque和back(见下文),但是有没有更好的方法呢?
b = deque(a)b.rotate(1)b = np.array(b)
3 个解决方案
#1
8
Just use the numpy.roll
function:
只使用numpy。功能:滚
a = np.array([0,1,2,3,4])b = np.roll(a,1)print(b)>>> [4 0 1 2 3]
See also this question.
看到这个问题。
#2
2
numpy.concatenate([a[-1:], a[:-1]])>>> array([4, 0, 1, 2, 3])
#3
1
Try this one
试试这个
b = a[-1:]+a[:-1]
#1
8
Just use the numpy.roll
function:
只使用numpy。功能:滚
a = np.array([0,1,2,3,4])b = np.roll(a,1)print(b)>>> [4 0 1 2 3]
See also this question.
看到这个问题。
#2
2
numpy.concatenate([a[-1:], a[:-1]])>>> array([4, 0, 1, 2, 3])
#3
1
Try this one
试试这个
b = a[-1:]+a[:-1]