I am working with numpy arrays and and I would like to move a certain value located at certain index by 2 positions on the right while shifting the others element involved on the left by one position each. To be more precise, here below is what I would like to achieve:
from:
start = [0., 1., 2.],
[0., 0., 0.],
[5., 0., 1.]
to:
end = [1., 2., 0.],
[0., 0., 0.],
[5., 0., 1.]
As you can see from the first row, 0 has been moved by two position on the right and 1 and 2 by one position on the left. So far, I succeded by moving an element by one position and moving the other by defining:
def right_move(arr, index, n_steps:int):
row = index[0]
col = index[1]
try:
arr[row,col], arr[row, col + n_steps] = arr[row, col + n_steps], arr[row, col]
except:
pass
return arr
where n_steps=1. If I input n_steps=2, it won't work since the element in the middle of the swapping remain unchanged.
Can someone help me? Other easier solution are more then welcome!
Thanks