Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse sort a 2d numpy array in python

Tags:

python

numpy

I have a numpy array like:

A = array([[-inf,  4,  5,  10, -inf, 1],
            [-inf,  2,  6, 8, -inf, 1],
            [-inf,  4,  -inf,  10, -inf, 100]
      ])

I need to sort in a decreasing order: 

A = array ([ 10,5,4,1,-inf,-inf], 
          [8,6,2,1,-inf,-inf],
           [100,10,4,-inf,-inf,-inf]])

Here -inf is float('-inf') How Do I do this?

I tried this: sorted(A, key=lambda listA: len(listA), reverse=True)

But I am not getting the sorted array. Can someone please tell me how to do this?

like image 953
learner Avatar asked Jul 10 '26 14:07

learner


1 Answers

How about

A.sort()
A[:,::-1]

?

References :

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.sort.html

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

like image 75
Jules G.M. Avatar answered Jul 13 '26 15:07

Jules G.M.



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!