Sometimes, we want to test whether a Numpy array contains a given row with Python.
In this article, we’ll look at how to test whether a Numpy array contains a given row with Python.
How to test whether a Numpy array contains a given row with Python?
To test whether a Numpy array contains a given row with Python, we can use the tolist
method.
For instance, we write
a = np.array([[1,2],[10,20],[100,200]])
in_list = [1, 2] in a.tolist()
to create an array with nested lists with np.array
.
Then we can check if [1, 2]
is in the list that’s returned by a.tolist
which converts the NumPy array a
back to to the original nested list.
And finally, we use the in
operator to check if [1, 2]
is in the nested list returned by tolist
which should return True
since [1, 2]
is in the original list that we used to create the a
array.
Conclusion
To test whether a Numpy array contains a given row with Python, we can use the tolist
method.