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

如何在python中从numpy数组中选择元素?

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

Python 中的 Numpy 数组

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

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

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

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

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

这些 ndarray 的每个元素都可以通过它们的索引号来访问。

立即学习“Python免费学习笔记(深入)”;

算法(步骤)

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

使用 import 关键字,导入带有别名 (np) 的 numpy 模块。

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

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

Use negative indexing to access the NumPy array element at index -1 i.e the last element of an array and print 它。

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, thesecond last has an index of -2, and so on.When you want to pick values from the end (right side) of an iterable, youcan utilize negative indexing to your benefit.

登录后复制

示例

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

# importing numpy module with an alias nameimport numpy as np# creating a 1-Dimensional NumPy arrayinputArray = 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: 5Numpy array element at index -1(last element): 8

登录后复制

方法 2 – 使用切片从 NumPy 数组中选择子数组

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

语法

numpyArray[start:stop]

登录后复制

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

算法(步骤)

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

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

通过给出起始值和终止值来访问从索引 2 到 5(不包括)的子数组using slicing and printing 它。

示例

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

# importing NumPy module with an alias nameimport numpy as np# creating a 1-Dimensional numpy arrayinputArray = 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 valuesprint("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 nameimport numpy as np# creating a 1-Dimensional NumPy arrayinputArray = 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 defaultprint("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 数组中从给定起始索引值到数组最后一个索引(默认)的子数组。

p>

# importing NumPy module with an alias nameimport numpy as np# creating a 1-Dimensional NumPy arrayinputArray = 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 valueprint("Input Array = ",inputArray)# It extends till the last index value by defaultprint("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 数组。

以上就是如何在Python中从Numpy数组中选择元素?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2225909.html

(0)
上一篇 2025年2月26日 11:27:24
下一篇 2025年2月25日 05:51:53

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • numpy数组拼接方法是什么

    numpy数组拼接方法有使用concatenate()函数、stack()函数和hstack()函数。详细介绍:1、concatenate()函数:该函数可以按照指定的轴将多个数组进行拼接;2、stack()函数:该函数可以按照指定的轴将多…

    2025年2月26日
    200

发表回复

登录后才能评论