python - 从该列的值检索二维 numpy 数组中的列索引

标签 python arrays numpy indexing

如果我有两个 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)

我能够找到解决方案,但我认为这应该是一些更直观的方法。

最佳答案

看看这个 answer 。 唯一的区别是您需要转置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

关于python - 从该列的值检索二维 numpy 数组中的列索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59585027/

相关文章:

python - 如何获取图像的二维直方图?

python - formencode Schema 动态添加字段

python - 如何手动将包安装到 anaconda 的 python 发行版中?

python - Pyopencl:to_device 和 Buffer 的区别

c++ - 数组、函数和随机数

javascript - 分机JS : Why can not call array from another class' property/function?

python - 覆盖第三方模块的方法有多糟糕?

python - Libimobiledevice python 绑定(bind)?

python - 寻找一种方法来阻止这个基本程序中的内存泄漏

javascript - javascript 中有没有一种好方法可以从 javascript 对象(而不是数组)中删除 Falsy 值?