m[:,0][(m[:,0] == n1) | (m[:,0] == n2)][0,0]
Explanation:
m = np.matrix([[1,1],
[2,0],
[3,1],
[5,1],
[5,0]])
n1 = 4; n2 = 1;
(m[:,0] == n1) returns a boolean matrix for n1's existence
matrix([[False],
[False],
[False],
[False],
[False]], dtype=bool)
(m[:,0] == n2) returns a boolean matrix for n2's existence
matrix([[ True],
[False],
[False],
[False],
[False]], dtype=bool)
Since you said that exactly a one of n1 and n2 parameters will be present at a time, |ing the above two will make the indices True for whatever the existing parameter.
(m[:,0] == n1) | (m[:,0] == n2)
matrix([[ True],
[False],
[False],
[False],
[False]], dtype=bool)
Indexing m[:,0] by the above boolean array,
m[:,0][(m[:,0] == n1) | (m[:,0] == n2)]
matrix([[1]])
We just get the first element out of it
m[:,0][(m[:,0] == n1) | (m[:,0] == n2)][0,0]
1
EDIT:
After numpy 1.13 +, as @John Zwink shows, you can compact the operations up to the last one as np.isin(m[:,0], [n1,n2])[:,0] and then just extract the first element out of it by np.where(np.isin(m[:,0], [n1,n2])[:,0])[0][0]
for value in m[:,0]: if value in [n1, n2]: return value. But I thought there might be something from numpy specifically for this. The numpy.isin shared by @user2357112 doesn't look like it would work as well.