 Introduction to Python NumPy Searching Array

Python Numpy Searching – Before moving ahead, let’s know a little bit about Python NumPy Splitting

Searching Array

Searching Arrays allows us to search any element from the Array and return the Array’s index number.

Use the where() method to search an array.

Example – Finding the index numbers where the value 11 is found.

```import numpy as np

Array = np.array([11, 12, 11, 11, 15, 14, 11])
x = np.where(Array == 11)

print(x)```
```Output –

(array([0, 2, 3, 6],)```

As a result, it returned an array containing index numbers of value 11 (Array ([0, 2, 3, 6],).

This means that the value 11 is presented at index 0, 2, 3, and 6.

Example – Finding the index numbers where the values are even:

```import numpy as np

Array = np.array([11, 12, 13, 14, 15, 16, 17, 18])
x = np.where(Array%2 == 0)

print(x)```
```Output –

(array([1, 3, 5, 7],)```

As shown above, it returned an array containing index numbers of even value (Array ([1, 3, 5, 7],).

This means that the even value is presented at indexes 1, 3, 5, and 7.

Example – Finding the index numbers where the values are odd:

```import numpy as np

Array = np.array([11, 12, 13, 14, 15, 16, 17, 18])
x = np.where(Array%2 != 0)

print(x)```
```Output –

(array([0, 2, 4, 6],)```

Henceforth, it returned an array containing index numbers of odd value (array([0, 2, 4, 6],)

This means that the odd value is presented at index 0, 2, 4, and 6.

Search Sorted

The searchsorted() method returns the Index number of a particular element that needs to be inserted in Array to maintain the sequence or search order of Array’s elements.

Example: Finding the index number where the value three should insert.

```import numpy as np

Array = np.array([1, 2, 3, 4])
x = np.searchsorted(Array, 3)

print(x)```
```Output –

2```

As shown above, it returned index number 2 because it is the index number where value three should insert to maintain the array sequence.

Note: The method starts the search from the left and returns the first value index where the specified number is not larger than the next value.

Search from the Right Side

The default is to return the leftmost index, but you can choose side=’right’ instead to return the rightmost index.

Example – Find the index number where the value 17 should be inserted, starting from the right:

```import numpy as np

Array = np.array([16, 17, 18, 19])
x = np.searchsorted(Array, 17, side='right')

print(x)```
```Output –

2```

As shown above, the method starts the search from the right and returns the index number 2, where the specified number is not larger than the next value.

Example – Find the index number where the value 17 should be inserted, starting from the left:

```import numpy as np

Array = np.array([16, 17, 18, 19])
x = np.searchsorted(Array, 17, side='left')

print(x)```
```Output –

1```

It has been noticed that the method starts the search from the left and returns the index number 1, where the specified number is not larger than the next value.

Multiple Values

An array with the specified values can be used to search for multiple values.

Example – Finding the index numbers where the values 12, 14, and 16 should be inserted:

```import numpy as np

Array = np.array([11, 13, 15, 17])
x = np.searchsorted(Array, [12, 14, 16])

print(x)```

The return value is an array [1 3 5] containing the three indexes 1, 3, 5, which would add to the original Array to maintain the order.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Like us on