如何在 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 陣列元素並列印它。

  • 使用負索引訪問索引 -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 陣列。

  • 透過使用切片給出 start 和 stop 值來訪問從索引 2 到 5(不包括 5)的子陣列並列印它。

示例

以下程式透過同時給出 start 和 stop 值,使用切片從輸入 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

示例

以下程式從索引 0(預設)到給定的結束值返回輸入 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 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 陣列。

更新於:2022年10月31日

18K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告