over.unity over.unity - 11 days ago 5
Python Question

finding in numpy pair values (without zeros) and get the keys

Suppose that I have a numpy like this

a = np.array([[0, 0, 0, 1373, 1371, 1365, 1371, 1375],
[0, 0, 0, 1388, 1371, 1365, 1301, 1302]])


I have to find all value pairs (compare idx 0 == 1).
for example:

pairs = (a[0] == a[1])
# [ True True True False True True False False]


but now, I'd like to have the idx's as result without comparing the zero values.
for example:

[4, 5]


This is an example, in my case I have around 100k datapoints. I'd like to find an efficient way.

Answer
In [14]: result = [r[0] for r in np.argwhere(a[0,:] == a[1,:]) if a[0, r] != 0]

In [15]: result
Out[15]: [4, 5]