將 Python 列錶轉換為 NumPy 數組

介紹

在 Python 中,列表是一種線性數據結構,可以存儲異構元素。它不需要定義,可以根據需要收縮和擴展。另一方面,NumPy 數組是一種可以存儲同質元素的數據結構。它是使用 NumPy 庫在 Python 中實現的。這個庫在處理多維數組時非常有效。它在處理大量數據元素時也非常高效。NumPy 數組使用的內存比 List 數據結構少。NumPy 數組和列表都可以通過它們的索引值來標識。

NumPy 庫提供了兩種在 Python 中將列錶轉換為數組的方法。

  1. 使用 numpy.array()
  2. 使用 numpy.asarray()

方法 1:使用 numpy.array()

在 Python 中,將列錶轉換為 NumPy 數組的最簡單方法是使用 numpy.array()函數。它接受一個參數並返回一個 NumPy 數組。它在內存中創建新的副本。

程序 1


# importing library of the array in python
import numpy
# initilizing elements of the list
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# converting elements of the list into array elements
arr = numpy.array(a)
# displaying elements of the list
print ("List: ", a)
# displaying elements of the array
print ("Array: ", arr)

輸出:

List:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
Array:  [1 2 3 4 5 6 7 8 9]

方法 2:使用 numpy.asarray()

在 Python 中,第二個方法是將列錶轉換為 numpy 數組的 numpy.asarray()函數。它接受一個參數並將其轉換為 NumPy 數組。它不會在內存中創建新副本。在這種情況下,對原始數組所做的所有更改都會反映在 NumPy 數組上。

程序 2


# importing library of the array in python
import numpy
# initilizing elements of the list
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# converting elements of the list into array elements
arr = numpy.asarray(a)
# displaying elements of the list
print ("List:", a)
# displaying elements of the array
print ("Array: ", arr)

輸出:

List:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
Array:  [1 2 3 4 5 6 7 8 9]

程序 3


# importing library of the NumPy array in python
import numpy
# initilizing elements of the list
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# converting elements of the list into array elements
arr = numpy.asarray(lst)
# displaying elements of the list
print ("List:", lst)
# displaying elements of the array
print ("arr: ", arr)
# made another array out of arr using asarray function
arr1 = numpy.asarray(arr)
#displaying elements of the arr1 before the changes made
print("arr1: " , arr1)
#change made in arr1
arr1[3] = 23
#displaying arr1 , arr , list after the change has been made
print("lst: " , lst)
print("arr: " , arr)
print("arr1: " , arr1)

輸出:

List: [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr:  [1 2 3 4 5 6 7 8 9]
arr1:  [1 2 3 4 5 6 7 8 9]
lst:  [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr:  [ 1  2  3 23  5  6  7  8  9]
arr1:  [ 1  2  3 23  5  6  7  8  9]


原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297262.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-28 12:15
下一篇 2024-12-28 12:15

相關推薦

  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智慧、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • Python編程二級證書考試相關現已可以上網購買

    計算機二級Python考試是一項重要的國家級認證考試,也是Python編程的入門考試。與其他考試一樣,Python編程二級證書的考生需要進入正式考試,而為了備考,這篇文章將詳細介紹…

    編程 2025-04-29
  • Python字元串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字元串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字元串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • 蝴蝶優化演算法Python版

    蝴蝶優化演算法是一種基於仿生學的優化演算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化演算法Python版…

    編程 2025-04-29

發表回復

登錄後才能評論