You are on page 1of 2

Arrays (Searching)

Searching Arrays
You can search an array for a certain value, and return the indexes that get a match.

To search an array, use the where() method.

#Find the indexes where the value is 4:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4,4])

arr = np.where(arr == 4)

print(arr)

(array([3, 5, 6, 7], dtype=int64),)

arr = ([1, 2, 3, 4, 5, 4, 4,4])


print(arr)
print(type(arr))

[1, 2, 3, 4, 5, 4, 4, 4]
<class 'list'>

#Find the indexes where the values are even:

import numpy as np

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

x = np.where(arr%2 == 0)

print(x)

(array([ 1, 3, 5, 7, 8, 9, 10], dtype=int32),)

Arrays (Searching) 1
#Find the indexes where the values are odd:

import numpy as np

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

x = np.where(arr%2 == 1)

print(arr[x])

[3 5 7]

Arrays (Searching) 2

You might also like