温馨提示×

NumPy如何查找特定值的索引

小亿
100
2024-05-13 12:37:16
栏目: 编程语言

要查找特定值的索引,可以使用np.where()函数。例如,要查找数组中值为5的索引,可以这样做:

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])
index = np.where(arr == 5)

print(index)

这将打印出值为5的索引,即array([4])。如果数组中有多个相同的值,np.where()将返回所有这些值的索引。

0