Numpy array sorting searching example

In this post, we learn how to search any element within numpy array, and then we learn how to sort them, sorting and searching array is most common function in any programming language.

numpy array search

There are two main functions for searching and sorting in numpy library.

  • numpy.where()
  • numpy.searchsorted()

Here we see how to search an element from one dimensional numpy array , we use where clause just like we used to write in any sql query .

import numpy as np

arr1 = np.array([1, 18, 32, 47, 52, 19, 84, 37])

result1= np.where(arr1==52)

print(result1)

Numpy search array example

The result will reveal the position of found element, like in above example i search number 52 in specified array, and the number was found in 4-index (array([4], dtype=int32),) position.

We can specify any clause within where clause, let’s look at the following examples to understand better.

In example below, we search all even numbers within the specified array.

import numpy as np

arr1 = np.array([1, 18, 32, 47, 52, 19, 84, 37])

result2 = np.where(arr1%2 == 0) #even

print(result2)
#(array([1, 2, 4, 6], dtype=int32),)

Just like above example we also can find all odd number from that same array just by changing the where clause like np.where(arr1%2 == 1) #odd, in where clause we can write any conditions like less then equal to, greater than equal to etc.

searchsorted function in mumpy

Searchsorted function can search and sort at the same time, there are different usage of that function, let’s look at examples below.

We will search one number within the specified array from left and from right side.

import numpy as np
arr1 = np.array([1, 18, 32, 47, 52, 19, 84, 37])

result4 = np.searchsorted(arr1, 47, side='left')

print(result4)

The above code will display where the number 47 located from left side, the result will show index-3 position, and instead of searching from left we can also search from right just by changing the side parameter like np.searchsorted(arr1, 47, side='right').

Now let’s look at another example where we want to position some numbers within an array, and need to find in which position they can be placed.

using searchsorted function we find index position of all specified numbers within an array.

import numpy as np

arr5 = np.array([4, 3, 5, 7])

result5= np.searchsorted(arr5, [2, 1, 6])

print(result5)

Above code will display result [0 0 3], means, new numbers [2, 1, 6] can be placed in [0 0 3] index positions in that specified array.

Sort numpy array

now we see how to sort numpy array, sorting can be ascending and descending order just like any other list object

import numpy as np

arr1 = np.array([1, 18, 32, 47, 52, 19, 84, 37])

result6 = np.sort(arr1)

print(result6)

result will be like [ 1 18 19 32 37 47 52 84]

If we apply sort method on 2d array, both array will be sorted. Here is how the sort function will work.

arr7 = np.array([[22, 12, 40], [15, 24, 11]])

print(np.sort(arr7)) 

result will be like [[12 22 40] [11 15 24]]

Sort string array in numpy program , sort method will rearrange all values in ascending order.

arr8 = np.array(['manago', 'banana', 'cucumber' , 'cherry', 'lichi' , 'apple'])
print(np.sort(arr8)) 

All string array value will be like ['apple' 'banana' 'cherry' 'cucumber' 'lichi' 'manago']

 
numpy array sorting
Free Tutorials
search array in numpy
Python NumPy examples | Join Python Course