w3resource

NumPy: Check whether a Numpy array contains a specified row

NumPy: Array Object Exercise-155 with Solution

Write a NumPy program to check whether a Numpy array contains a specified row.

Sample Solution:

Python Code:

import numpy as np
num = np.arange(20)
arr1 = np.reshape(num, [4, 5])
print("Original array:")
print(arr1)
print([0, 1, 2, 3, 4] in arr1.tolist())
print([0, 1, 2, 3, 5] in arr1.tolist())
print([15, 16, 17, 18, 19] in arr1.tolist())

Sample Output:

Original array:
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]
True
False
True

Pictorial Presentation:

NumPy: Check whether a Numpy array contains a specified row

Python Code Editor:

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

Previous: Write a NumPy program to get a copy of a matrix with the elements below the k-th diagonal zeroed.
Next: Write a NumPy program to calculate averages without NaNs along a given array.

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}