Sorting values in an array: 'reverse' is an invalid keyword argument for this function

14,238

You couldn't use the key or reverse keyword arguments according to numpy documentation. You can sort the array in ascending order and then reverse it with the [::-1] slice or use the reversed() view.

Share:
14,238
S Verhoef
Author by

S Verhoef

Student @Utrecht University

Updated on June 15, 2022

Comments

  • S Verhoef
    S Verhoef almost 2 years

    I try to sort values in an array in descending order. If I try it in ascending order it works, but when i do it descending I get an error.

    e = np.array([[5.,3.,8.],[6.,7.,1.],[4.,8.,2.]])
    e.sort()
    

    result:

    e = array([[3.,5.,8.],[1.,6.,7.],[2.,4.,8.]])
    

    now in reverse order:

      e.sort(reverse=True)
    

    result:

    TypeError: 'reverse' is an invalid keyword argument for this function
    

    I also tried e.sort(key=itemgetter(1)) after from operator import itemgetter but the same error appears ('reverse' is replaced by 'key').

    Why is this the case? Why does it not work? Why this error (this is the way to use key or reverse right)?

    • S Verhoef
      S Verhoef over 6 years
      I've found my result here: link f = -np.sort(-e) worked perfect!
  • S Verhoef
    S Verhoef over 6 years
    I've also some nan values in my dataset which should stay at the end of the array. By using fliplr these would be put in front. Otherwise this would be perfect.
  • S Verhoef
    S Verhoef over 6 years
    No column is of special interest, all rows should be sorted indivudually.
  • mgilson
    mgilson over 6 years
    @SVerhoef -- I guess I don't understand what you're getting at with they key=itemgetter(1) then ...