简体   繁体   English

从此列的值中检索 2d numpy 数组中的列索引

[英]Retrieve column index in 2d numpy array from the value of this column

If I have two numpy arrays like:如果我有两个 numpy 数组,例如:

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 .我想获得与b值对应的a列的索引。 Here it would be 0.这里应该是 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

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM