numpy array operation

Peter Otten __peter__ at web.de
Tue Jan 29 04:28:02 EST 2013


C. Ng wrote:

> Is there a numpy operation that does the following to the array?
> 
> 1 2  ==>  4 3
> 3 4       2 1

How about

>>> a
array([[1, 2],
       [3, 4]])
>>> a[::-1].transpose()[::-1].transpose()
array([[4, 3],
       [2, 1]])

Or did you mean

>>> a.reshape((4,))[::-1].reshape((2,2))
array([[4, 3],
       [2, 1]])

Or even

>>> -a + 5
array([[4, 3],
       [2, 1]])





More information about the Python-list mailing list