Checking If An Array Is Element Of A List Of Arrays
I have 13 python NumPy arrays: obj_1=np.array([784,785,786,787,788,789,790,791,792]) obj_2=np.array([716,717,718,730,731,732,721,722,724,726,727]) obj_3=np.array([658,659,660,661,6
Solution 1:
When you do obj in list
python compares obj
for equality with each of the members of list. The problem is that the equality operator in numpy doesn't behave in a consistent way. For example:
>>> obj_3 == obj_6
False
because they have different lengths, a boolean is returned.
>>> obj_7==obj_6
array([False, False, False, False], dtype=bool)
because they have the same length, the arrays are compared element wise and a numpy array is returned. In this case the truth value is undetermined. It looks like this strange behaviour is going to change in the future.
The correct way to do it would be to compare individually each pair of arrays using for example numpy.array_equal:
for i in [obj_1, obj_2, obj_3, obj_4, obj_5, obj_6, obj_7, obj_8, obj_9, obj_10, obj_11, obj_12, obj_13]:
print any(np.array_equal(i, j) for j in [obj_1, obj_2, obj_3, obj_4, obj_5, obj_6, obj_7, obj_8, obj_9])
gets you:
True
True
True
True
True
True
True
True
True
False
False
False
False
Post a Comment for "Checking If An Array Is Element Of A List Of Arrays"