Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: how to move values in an array from some position to another?

Tags:

python

arrays

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])
like image 717
emax Avatar asked Mar 31 '26 17:03

emax


1 Answers

You can use slicing and concatenation.

np.concatenate((a[:3], a[-3:], a[3:-3]))
like image 79
Sven Krüger Avatar answered Apr 02 '26 07:04

Sven Krüger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!