Issue
For example, I want, given the list
given_list = [np.array([1,44,21,2])]
Check if this list contains another numpy array, for example np.array([21,4,1,21])
I tried this but it checks if each element of the numpy array is the equal to the new numpy array
import numpy as np
given_list = [np.array([1,44,21,2])]
new_elem = np.array([21,4,1,21])
print([new_elem==elem for elem in given_list])
I would like to receive '[False]', but instead I get '[array([False, False, False, False])]'.
I don't understand why given_list
is identified as a numpy array and not as a list of one numpy array.
Solution
You can use numpy.array_equal
:
[np.array_equal(new_elem, elem) for elem in given_list]
or numpy.allclose
if you want to have some tolerance:
[np.allclose(new_elem, elem) for elem in given_list]
output: [False]
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.