如何在 Python 中从 Numpy 数组中选择元素?

pythonserver side programmingprogramming更新于 2024/2/18 11:01:00

在本文中,我们将向您展示如何在 Python 中从 NumPy 数组中选择元素。

Python 中的 Numpy 数组

顾名思义,NumPy 数组是 NumPy 库的中心数据结构。该库的名称是"Numeric Python"或"Numerical Python"的缩写。

换句话说,NumPy 是一个 Python 库,是 Python 中科学计算的基础。这些工具之一是高性能多维数组对象,它是一种用于高效数组和矩阵计算的强大数据结构。

我们可以一次从 Numpy 数组中选择一个元素或一个子数组。现在我们看到以下从 Numpy 数组中选择元素的方法。

  • 选择单个 NumPy 数组元素
  • 使用切片从 NumPy 数组中选择子数组
  • 通过仅提供停止值来选择/访问子数组
  • 通过仅提供起始值来选择/访问子数组

方法 1 - 选择单个NumPy 数组元素

这些 ndarrays 的每个元素都可以通过其 索引 号访问。

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 import 关键字,以别名 (np) 导入 numpy 模块。

  • 使用 numpy.array() 函数(返回一个 ndarray。 ndarray 是一个满足给定要求的数组对象),用于通过将一维数组作为参数传递给它来创建 numpy 数组。

  • 使用正索引访问索引 1 处的 NumPy 数组元素并打印它。

  • 使用负索引访问索引 -1 处的 NumPy 数组元素,即数组的最后一个元素并打印它。

Negative Indexing():
Python allows for "indexing from the end," i.e., negative indexing.
This means that the last value in a sequence has an index of -1, the
second last has an index of -2, and so on.
When you want to pick values from the end (right side) of an iterable, you
can utilize negative indexing to your benefit.

示例

以下程序使用索引号从输入 NumPy 数组返回指定索引处的元素 -

# importing numpy module with an alias name import numpy as np # creating a 1-Dimensional NumPy array inputArray = np.array([4, 5, 1, 2, 8]) # printing the array element at index 1 (positive indexing) print("The input array = ",inputArray) print("Numpy array element at index 1:", inputArray[1]) # printing the array element at index -1 i.e last element (negative indexing) print("Numpy array element at index -1(last element):", inputArray[-1])

输出

执行时,上述程序将生成以下输出 -

The input array =  [4 5 1 2 8]
Numpy array element at index 1: 5
Numpy array element at index -1(last element): 8

方法 2 - 使用切片从 NumPy 数组中选择一个子数组

为了获得子数组,我们用切片代替元素索引。

语法

numpyArray[start:stop]

其中,start、stop 分别是子数组的第一个和最后一个索引。

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 numpy.array() 函数(返回一个 ndarray。ndarray 是一个满足给定要求的数组对象),通过传递一维数组来创建 NumPy 数组作为它的参数。

  • 通过使用切片并打印它,通过给出起始值和终止值来访问从索引 2 到 5(不包括)的子数组。

示例

以下程序通过给出起始值和终止值,使用切片从输入 NumPy 数组返回子数组 -

# importing NumPy module with an alias name import numpy as np # creating a 1-Dimensional numpy array inputArray = np.array([4, 5, 1, 2, 8, 9, 7]) print("Input Array =",inputArray) # printing the sub-array from index 2 to 5(excluded) by giving start, stop values print("The sub-array from index 2 to 5(excluded)=", inputArray[2:5])

输出

执行时,上述程序将生成以下输出 -

Input Array = [4 5 1 2 8 9 7]
The sub-array from index 2 to 5(excluded)= [1 2 8]

方法 3 - 通过仅提供停止值来选择/访问子数组

通过将起始索引留空,您可以从第一个元素开始对子数组进行切片。

默认情况下,起始值为 0

示例

以下程序从输入 NumPy 数组中返回从索引 0(默认)到给定停止值的子数组 -

# importing NumPy module with an alias name import numpy as np # creating a 1-Dimensional NumPy array inputArray = np.array([4, 5, 1, 2, 8, 9, 7]) print("Input Array =",inputArray) # printing the sub-array till index 5(excluded) by giving only stop value # it starts from index 0 by default print("The sub-array till index 5(excluded)=", inputArray[:5])

输出

执行时,上述程序将生成以下输出 -

Input Array = [4 5 1 2 8 9 7]
The sub-array till index 5(excluded)= [4 5 1 2 8]

方法 4 - 通过仅提供起始值来选择/访问子数组

同样,将冒号左侧留空将为您提供一个直到最后一个元素的数组。

示例

以下程序从输入 NumPy 数组中返回从给定起始索引值到数组的最后一个索引(默认)的子数组。

# importing NumPy module with an alias name import numpy as np # creating a 1-Dimensional NumPy array inputArray = np.array([4, 5, 1, 2, 8, 9, 7]) # printing the sub-array from index 2 to the last index by giving only the start value print("Input Array = ",inputArray) # It extends till the last index value by default print("The sub-array till index 5(excluded)=", inputArray[2:])

输出

执行时,上述程序将生成以下输出 -

Input Array = [4 5 1 2 8 9 7]
The sub-array till index 5(excluded)= [1 2 8 9 7]

结论

在本文中,我们通过四个不同的示例学习了如何在 Python 中选择 numpy 数组的元素。我们还学习了切片 Numpy 数组。


相关文章