卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16760本站已运行3321

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

如何在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 它。

<font color=\"#660066\">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.</font>

示例

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

 

# importing numpy module with an alias name</span>
import</span> numpy as</span> np

# creating a 1-Dimensional NumPy array</span>
inputArray =</span> np.</span>array(</span>[</span>4</span>,</span> 5</span>,</span> 1</span>,</span> 2</span>,</span> 8</span>]</span>)</span>

# printing the array element at index 1 (positive indexing)</span>
print</span>(</span>\"The input array = \"</span>,</span>inputArray)</span>
print</span>(</span>\"Numpy array element at index 1:\"</span>,</span> inputArray[</span>1</span>]</span>)</span>

# printing the array element at index -1 i.e last element (negative indexing)</span>
print</span>(</span>\"Numpy array element at index -1(last element):\"</span>,</span> inputArray[</span>-</span>1</span>]</span>)</span>

输出

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

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</span>
import</span> numpy as</span> np

# creating a 1-Dimensional numpy array</span>
inputArray =</span> np.</span>array(</span>[</span>4</span>,</span> 5</span>,</span> 1</span>,</span> 2</span>,</span> 8</span>,</span> 9</span>,</span> 7</span>]</span>)</span>
print</span>(</span>\"Input Array =\"</span>,</span>inputArray)</span>

# printing the sub-array from index 2 to 5(excluded) by giving start, stop values</span>
print</span>(</span>\"The sub-array from index 2 to 5(excluded)=\"</span>,</span> inputArray[</span>2</span>:</span>5</span>]</span>)</span>

输出

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

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</span>
import</span> numpy as</span> np

# creating a 1-Dimensional NumPy array</span>
inputArray =</span> np.</span>array(</span>[</span>4</span>,</span> 5</span>,</span> 1</span>,</span> 2</span>,</span> 8</span>,</span> 9</span>,</span> 7</span>]</span>)</span>
print</span>(</span>\"Input Array =\"</span>,</span>inputArray)</span>

# printing the sub-array till index 5(excluded) by giving only stop value</span>

# it starts from index 0 by default</span>
print</span>(</span>\"The sub-array till index 5(excluded)=\"</span>,</span> inputArray[</span>:</span>5</span>]</span>)</span>

输出

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

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</span>
import</span> numpy as</span> np

# creating a 1-Dimensional NumPy array</span>
inputArray =</span> np.</span>array(</span>[</span>4</span>,</span> 5</span>,</span> 1</span>,</span> 2</span>,</span> 8</span>,</span> 9</span>,</span> 7</span>]</span>)</span>

# printing the sub-array from index 2 to the last index by giving only the </span>start value
print</span>(</span>\"Input Array = \"</span>,</span>inputArray)</span>
# It extends till the last index value by default</span>
print</span>(</span>\"The sub-array till index 5(excluded)=\"</span>,</span> inputArray[</span>2</span>:</span>]</span>)</span>

输出

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

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程序从给定字符串中删除后缀
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏