Python 中 2d np.array 的数组索引?
Index of an array from a 2d np.array in Python?
我在从 2d np.array()
中查找索引时遇到问题。对于 If 循环,计算时间对于我的算法来说太长了。我需要一个 numpy func 来从任何数组中找到索引。函数 np.where()
对我没有帮助。这是一个抽象:
>>> conditions = [[0 0 0 0 4]
[0 0 0 0 3]
[0 0 0 0 2]
...,
[2 1 3 4 2]
[2 1 3 4 1]
[2 1 3 4 0]]
>>> a = [0, 0, 0, 0, 2]
>>> index = np.where(conditions==a)
如果我这样使用它,虽然我有列索引和行索引,但我无法解释它们。我需要具体的索引值,例如 index = 2
.
>>> np.where((conditions == a).all(axis=1))[0]
array([2])
我在从 2d np.array()
中查找索引时遇到问题。对于 If 循环,计算时间对于我的算法来说太长了。我需要一个 numpy func 来从任何数组中找到索引。函数 np.where()
对我没有帮助。这是一个抽象:
>>> conditions = [[0 0 0 0 4]
[0 0 0 0 3]
[0 0 0 0 2]
...,
[2 1 3 4 2]
[2 1 3 4 1]
[2 1 3 4 0]]
>>> a = [0, 0, 0, 0, 2]
>>> index = np.where(conditions==a)
如果我这样使用它,虽然我有列索引和行索引,但我无法解释它们。我需要具体的索引值,例如 index = 2
.
>>> np.where((conditions == a).all(axis=1))[0]
array([2])