-1

I have an array like this:

array([[-1.2825],
       [-0.9202],
       [-0.0809],
       [-0.1469],
       [-0.3981],
       [-0.1422],
       [-0.0132]])

and I would like the first element to be the last, second element to be the second from the end etc. How can I do this?

array([[-0.0132],
       [-0.1422],
       [-0.3981],
       [-0.1469], 
       [-0.0809],
       [-0.9202],
       [-1.2825]])
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Mr.Price
  • 109
  • 5

3 Answers3

1

Assuming its list or numpy:

a= a[::-1]
adir abargil
  • 5,495
  • 3
  • 19
  • 29
1

You can use the flip function from numpy:

import numpy as np
yourarray = np.array([[-0.0132],
       [-0.1422],
       [-0.3981],
       [-0.1469], 
       [-0.0809],
       [-0.9202],
       [-1.2825]])  
reversed_array = np.flip(yourarray)
adminpaz
  • 116
  • 8
  • Thanks! Can you maybe know how can I change now this array into one dimensional array of the form: `np.array([-0.0132, -0.1422, -0.3981, -0.1469, -0.0809, -0.9202, -1.2825]) ` i.e without brackets? – Mr.Price Dec 02 '20 at 18:52
  • Yes, you can use the flatten method: `reversed_array.flatten()` – adminpaz Dec 02 '20 at 20:25
0

Try this:

for i in range(len(array)):
    if i + i + 1 > len(array):
        break
    array[i], array[-i - 1] = array[-i - 1], array[i]
  • This is a really bad solution. Take a look at the other solutions to see how to revert a list or an array. – JE_Muc Dec 02 '20 at 17:59