a.sort (), sorted (a), np.argsort (a) and np.lexsort (b, a) in Python

Let & # 39; a & # 39; be an array

# Python code to sort the array in place
# using a.sort

import numpy as np

 
# Numpy array created

a = np.array ([ 9 , 3 , 1 , 7 , 4 , 3 , 6 ])

  
# unsorted print array

print ( ` Original array: ` , a)

  
# Return type - None

print ( `Return type:` , a.sort ())

 
# Sorted output array

print ( `Original array sorted- & gt;` , a)

 OUTPUT: For a.sort () Original array: [9 3 1 7 4 3 6] Return type: None Original array sorted- & gt; [1 3 3 4 6 7 9] 
  • sorted (a)
    (i) Creates a new list from the old and returns a new sorted
    (ii) Return type — list
    (iii) Takes up more space because a copy of the original array is created and then sorted. 
    (iv) Slower than a.sort ()

    # Python code to create a sorted copy using
    # sorted ()

    import numpy as np

     
    # Numpy array created

    a = np.array ([ 9 , 3 , 1 , 7 , 4 , 3 , 6 ])

      
    # unsorted print array

    print ( `Original array:` , a)

    b = sorted (a)

     
    # the sorted list is returned to b, type b
    # & lt; class & # 39; list & # 39; & gt;

    print ( `New array sorted- & gt;` , b)

     
    # original array unchanged

    print ( ` Original array- & gt; ` , a )

     OUTPUT: a.sorted () Original array: [9 3 1 7 4 3 6] New array sorted - & gt; [1, 3, 3, 4, 6, 7, 9] Original array- & gt; [9 3 1 7 4 3 6] 
  • np.argsort (a)
    (i) Returns the indices that will sort the array
    (ii) Return type — numpy array
    (iii) Takes up space as a new array of sorted indices is returned.

    # Python code to demonstrate how np.argsort works

    import numpy as np

     
    # Numpy array created

    a = np.array ([ 9 , 3 , 1 , 7 , 4 , 3 , 6 ] )

      
    # unsorted print array

    print ( `Original array:` , a)

     
    # Sort array indices

    b = np.argsort (a)

    print ( ` Sorted indices of original array- & gt; ` , b)

     
    # Get a sorted array using sorted indices
    # c is a temporary array created from the same len as b

    c = np.zeros ( len (b), dtype = int )

    for i in range ( 0 , len (b)):

    c [i] = a [b [i]]

    print ( ` Sorted array- & gt; ` , c)

     OUTPUT: np.argsort (a) Original array: [9 3 1 7 4 3 6] Sorted indices of original array- & gt; [2 1 5 4 6 3 0] Sorted array- & gt; [1 3 3 4 6 7 9] 
  • np.lexsort ((b, a))
    (i) Perform indirect sort using sequence keys
    (ii) Sort by a, then by b
    (iii) Return type ndarray of ints Array of indices that sort the keys along the specified axis
    (iv) Takes up space as a new one is returned array of sorted indices.

     OUTPUT: np.lexsort ((b, a)) column a, column b 9 4 3 6 1 9 3 2 4 1 3 8 6 7 Sorted indices - & gt; [2 3 1 5 4 6 0] 

    This article courtesy of SHAURYA UPPAL . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

    Please post comments if you find anything wrong or if you`d like to share more information on the topic discussed above.


    # Python code to demonstrate how it works
    # np.lexsort ()

    import numpy as np

     
    # Numpy array created

    a = np.array ([ 9 , 3 , 1 , 3 , 4 , 3 , 6 ]) # First column

    b = np.array ([ 4 , 6 , 9 , 2 , 1 , 8 , 7 ]) # Second column

    print ( `column a, column b` )

    for (i, j) in zip (a, b):

    print (i, ` ` , j)

      

    ind = np.lexsort ((b, a)) # Sort by then by b

    print ( `Sorted indices- & gt;` , ind)