I have a numpy array (a list of 2 elements lists) a
give below and I have a list of 2 elements [30.94, 0.]
that I would like to look for.
When I did the following I don't get the desired result. Why?
import numpy as np
a = np.array([[ 5.73, 0. ],
[ 57.73, 10. ],
[ 57.73, 20. ],
[ 30.94, 0. ],
[ 30.94, 10. ],
[ 30.94, 20. ],
[ 4.14, 0. ],
[ 4.14, 10. ]])
np.where(a==np.array([30.94, 0.]))
But I get
(array([0, 3, 3, 4, 5, 6]), array([1, 0, 1, 0, 0, 1]))
which is not true.
As Divakar hinted,
a == np.array([30.94, 0.])
is not what you expect. The array is broadcast, and the comparison is done elementwise. Here is the result:However, we can get what we want with
np.all
:So you can see that row 3 matches, as expected. Note that the usual caveats to using
==
with floating-point numbers will apply here.