numpy array operation

C

C. Ng

Is there a numpy operation that does the following to the array?

1 2 ==> 4 3
3 4 2 1

Thanks in advance.
 
T

Terry Reedy

Is there a numpy operation that does the following to the array?

1 2 ==> 4 3
3 4 2 1

Thanks in advance.

How about:
import numpy as np
a = np.array([[1,2],[3,4]])
a array([[1, 2], [3, 4]])
a[::-1, ::-1]
array([[4, 3], [2, 1]])

Nice. The regular Python equivalent is

a = [[1,2],[3,4]]
print([row[::-1] for row in a[::-1]])[[4, 3], [2, 1]]

The second slice can be replaced with reversed(a), which returns an
iterator, to get
[row[::-1] for row in reversed(a)]
The first slice would have to be list(reversed(a)) to get the same result.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,795
Messages
2,569,644
Members
45,356
Latest member
deepthi.kodakandla

Latest Threads

Top