如何在Python中从Numpy数组中选择元素?
在本文中,我们将向您展示如何在 Python 中从 NumPy 数组中选择元素。
Python 中的 Numpy 数组
顾名思义,NumPy 数组是 NumPy 库的中心数据结构。该库的名称是“Numeric Python”或“Numerical Python”的缩写。
换句话说,NumPy 是一个 Python 库,是 Python 科学计算的基础。其中一个工具是高性能多维数组对象,它是一种用于高效数组和矩阵计算的强大数据结构。我们可以一次从 Numpy 数组中选择一个元素或一个子数组。现在我们看到以下从 Numpy 数组中选择元素的方法。
- 选择单个 NumPy 数组元素
- 使用切片从 NumPy 数组中选择子数组
- 仅通过给出停止值来选择/访问子数组
- 仅通过给出起始值来选择/访问子数组
方法 1 - 选择单个 NumPy 数组元素
这些 ndarray 的每个元素都可以通过它们的索引号来访问。
算法(步骤)
以下是执行所需任务所需遵循的算法/步骤 -
使用 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, 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(不包括)的子数组 using slicing and printing 它。
示例
以下程序通过给出开始值和停止值,使用切片从输入 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 数组中从给定起始索引值到数组最后一个索引(默认)的子数组。
p>
# 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 数组。
以上是如何在Python中从Numpy数组中选择元素?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。 Python以简洁和强大的生态系统着称,C 则以高性能和底层控制能力闻名。

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在游戏和GUI开发中表现出色。1)游戏开发使用Pygame,提供绘图、音频等功能,适合创建2D游戏。2)GUI开发可选择Tkinter或PyQt,Tkinter简单易用,PyQt功能丰富,适合专业开发。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。

要在有限的时间内最大化学习Python的效率,可以使用Python的datetime、time和schedule模块。1.datetime模块用于记录和规划学习时间。2.time模块帮助设置学习和休息时间。3.schedule模块自动化安排每周学习任务。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

Python在自动化、脚本编写和任务管理中表现出色。1)自动化:通过标准库如os、shutil实现文件备份。2)脚本编写:使用psutil库监控系统资源。3)任务管理:利用schedule库调度任务。Python的易用性和丰富库支持使其在这些领域中成为首选工具。
