If I have two numpy arrays like:
a = np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
b = np.array([ 0, 4, 8])
and I would like to get the index of the column of a that corresponds to the values of b . Here it would be 0.
With something like:
np.where(np.hsplit(a, 4) == b)
I'm able to find the solution but I think it should be some more intuitive way of doing so.
我不知道它是否或多或少直观,但您可以转置a
并进行比较:
np.where( (a.transpose() == b ).all(axis=1))
Take a look at this answer . The only difference is that you need to transpose a
.
>>> a = np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> b = np.array([ 0, 4, 8])
>>> np.all(a.T==b,axis=1)
array([ True, False, False, False])
>>> np.where(np.all(a.T==b,axis=1))[0][0]
0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.