📜  如何在 Numpy Array 中找到值的索引?

📅  最后修改于: 2022-05-13 01:54:59.940000             🧑  作者: Mango

如何在 Numpy Array 中找到值的索引?

在本文中,我们将查找 NumPy 数组中存在的元素的索引。 where()方法用于指定条件中指定的特定元素的索引。

句法:

示例 1:



在这个程序中,我们将使用 NumPy 创建一个数组并显示它。

我们可以使用以下语法使用 numpy 创建一个数组:

numpy.array([value1,value2,value3,.....,value n])
Python3
# import numpy package
import numpy as np
  
# create an numpy array with 1 
# to 10 elements
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  
print(a)


Python3
# display index value
# of 3
print(np.where(a == 3))


Python3
# display index value of 9
print(np.where(a == 9))


Python3
# Create a numpy array from a list of numbers
# from 11 to 20
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
                11, 12, 14, 15, 16, 17, 18, 19, 20])
  
# Get the index of elements with value less 
# than 20 and greater than 12
print("The numbers index locations with the index of \
elements with value less than 20 and greater than 12 are ",
      np.where((a > 12) & (a < 20)))


Python3
# Create a numpy array from a list of 
# numbers from 11 to 20
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
              11, 12, 14, 15, 16, 17, 18, 19, 20])
  
# Get the index of elements with value less 
# than 20 or greater than 12
print("The numbers index locations with the index of \
elements with value less than 20 or  greater than 12 are ",
      np.where((a > 12) | (a < 20)))


Python3
# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
  
# display element index where values 
# less than 20
print("element index where values less than 20 : ", 
      np.where(a < 20))


Python3
# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
  
# display element index where values 
# greater than 20
print("element index where values greater than 20 : ", 
      np.where(a > 20))


输出:

[ 1  2  3  4  5  6  7  8  9 10]

找到 3 的索引

蟒蛇3

# display index value
# of 3
print(np.where(a == 3))

输出:

(array([2], dtype=int64),)

查找索引值为 9



蟒蛇3

# display index value of 9
print(np.where(a == 9))

输出:

(array([8], dtype=int64),)

我们还可以根据多个条件获取元素的索引。这些条件在 where()函数中指定。

示例 2:

获取值小于 20 且大于 12 的元素的索引

蟒蛇3

# Create a numpy array from a list of numbers
# from 11 to 20
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
                11, 12, 14, 15, 16, 17, 18, 19, 20])
  
# Get the index of elements with value less 
# than 20 and greater than 12
print("The numbers index locations with the index of \
elements with value less than 20 and greater than 12 are ",
      np.where((a > 12) & (a < 20)))

输出:

获取值小于 20 且大于 12 的元素的索引

蟒蛇3



# Create a numpy array from a list of 
# numbers from 11 to 20
a = np.array([11, 12, 13, 14, 15, 16, 17, 15,
              11, 12, 14, 15, 16, 17, 18, 19, 20])
  
# Get the index of elements with value less 
# than 20 or greater than 12
print("The numbers index locations with the index of \
elements with value less than 20 or  greater than 12 are ",
      np.where((a > 12) | (a < 20)))

输出:

示例 3:

显示值小于 20 的元素索引

蟒蛇3

# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
  
# display element index where values 
# less than 20
print("element index where values less than 20 : ", 
      np.where(a < 20))

输出:

示例 4:

显示值大于 20 的元素索引

蟒蛇3

# create numpy array elements
a = np.array([2, 3, 4, 5, 6, 45, 67, 34])
  
# display element index where values 
# greater than 20
print("element index where values greater than 20 : ", 
      np.where(a > 20))

输出: