I have an a array of values
a = np.array([0,3,4,5,12,3,78,53,52])
I would like to move the last three elements in the array starting from the index 3 in order to have
a
array([ 0, 3, 4, 78, 53, 52, 5, 12, 3])
You can use slicing and concatenation.
np.concatenate((a[:3], a[-3:], a[3:-3]))
This is just a number-swapping problem -- not a numpy problem.
Any solution to this problem that involves numpy functions such as concatenate, delete, insert, or even slicing, is inefficient, involving unnecessary copying of data.
This should work, with minimum copying of data:
a[3],a[4],a[5], a[-3],a[-2],a[-1] = a[-3],a[-2],a[-1], a[3],a[4],a[5]
print(a)
Output:
[ 0 3 4 78 53 52 5 12 3]