python - Finding whether a list contains a particular numpy array -
import numpy np = np.eye(2) b = np.array([1,1],[0,1]) my_list = [a, b]
a in my_list
returns true
, b in my_list
returns "valueerror: truth value of array more 1 element ambiguous. use a.any() or a.all()". can around converting arrays strings or lists first, there nicer (more pythonic) way of doing it?
the problem in numpy ==
operator returns array:
>>> == b array([[ true, false], [ true, true]], dtype=bool)
you use .array_equal()
compare arrays pure boolean value.
>>> any(np.array_equal(a, x) x in my_list) true >>> any(np.array_equal(b, x) x in my_list) true >>> any(np.array_equal(np.array([a, a]), x) x in my_list) false >>> any(np.array_equal(np.array([[0,0],[0,0]]), x) x in my_list) false
Comments
Post a Comment