w3resource

NumPy: Search the index of a given array in another given array

NumPy: Array Object Exercise-171 with Solution

Write a NumPy program to search the index of a given array in another given array.

Sample Solution:

Python Code:

import numpy as np
np_array = np.array([[1,2,3], [4,5,6] , [7,8,9], [10, 11, 12]])
test_array = np.array([4,5,6])
print("Original Numpy array:")
print(np_array)
print("Searched array:")
print(test_array)
print("Index of the searched array in the original array:")
print(np.where((np_array == test_array).all(1))[0])

Sample Output:

Original Numpy array:
[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]
Searched array:
[4 5 6]
Index of the searched array in the original array:
[1]

Pictorial Presentation:

NumPy: Search the index of a given array in another given array

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to get all 2D diagonals of a 3D NumPy array.
Next: Write a Numpy program to find and store non-zero unique rows in an array after comparing each row with other row in a given matrix.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}