[英]Retrieve column index in 2d numpy array from the value of this column
如果我有两个 numpy 数组,例如:
a = np.array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
b = np.array([ 0, 4, 8])
我想获得与b值对应的a列的索引。 这里应该是 0。
像这样:
np.where(np.hsplit(a, 4) == b)
我能够找到解决方案,但我认为这应该是一些更直观的方法。
我不知道它是否或多或少直观,但您可以转置a
并进行比较:
np.where( (a.transpose() == b ).all(axis=1))
看看这个答案。 唯一的区别是,你需要转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.