3

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?

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Since his array is 2D, it'll be rather [:,::-1].
I am getting :TypeError: 'NoneType' object is not subscriptable
A.sort() sorts array in place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.