將Python字典轉換為NumPy陣列的不同方法
NumPy是Python中流行的庫之一,用於執行數值計算和科學計算。它還允許我們處理大型多維陣列和矩陣。NumPy庫中內建了許多函式和模組,用於處理不同維度的陣列。
將字典轉換為NumPy陣列
我們可以使用NumPy庫中提供的不同模組和函式將字典轉換為NumPy陣列。讓我們逐一檢視每種方法。
使用numpy.array()函式
在NumPy中,我們有一個名為array()的函式,它用於透過傳遞任何資料結構(例如字典、列表等)來建立陣列。以下是使用NumPy庫的array()函式將Python字典轉換為NumPy陣列的語法。
import numpy numpy.array(dictionary_name)
其中:
numpy是庫的名稱。
array是建立陣列的函式。
dictionary_name是作為輸入引數的字典。
示例
在這個示例中,我們將使用array()函式透過傳遞字典作為輸入引數來建立NumPy陣列,然後字典將被轉換為NumPy陣列。
import numpy as np
dic = {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'],
'Age': [1, 30, 25, 1, 55],
'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
print("The dictionary to be converted into array:",dic)
arr = np.array(list(dic.values()))
print("The array created from the dictionary:",arr)
print(type(arr))
輸出
The dictionary to be converted into array: {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
The array created from the dictionary: [['Karthikeya' 'Anil' 'Srivatsav' 'Riyaansh' 'Prasad']
['1' '30' '25' '1' '55']
['python' 'Salesforce' 'Networking' 'python' 'Management']]
<class 'numpy.ndarray'>
使用numpy.asarray()函式
在numpy庫中,我們還有另一個名為asarray()的函式,它具有與array()函式相似的功能。此函式也採用字典值作為輸入並返回NumPy陣列作為輸出。以下是使用asarray()函式的語法。
np.asarray(dictionary)
示例
在下面的示例中,我們將字典作為輸入引數傳遞給array()函式,以將字典轉換為NumPy陣列。
import numpy as np
dic = {'Age': [1, 30, 25, 1, 55],
'year': [2002,2020,1995,1900,2023]}
print("The dictionary to be converted into array:",dic)
arr = np.asarray(list(dic.values()))
print("The array created from the dictionary:",arr)
print(type(arr))
輸出
The dictionary to be converted into array: {'Age': [1, 30, 25, 1, 55], 'year': [2002, 2020, 1995, 1900, 2023]}
The array created from the dictionary: [[ 1 30 25 1 55]
[2002 2020 1995 1900 2023]]
<class 'numpy.ndarray'>
使用迴圈
使用迴圈,我們可以將字典轉換為NumPy陣列。以下是將字典轉換為NumPy陣列的示例。
import numpy as np
dic = {'Age': 20, 'year': 2023,'class':10}
arr = np.zeros(len(dic))
i = 0
for key in dic:
arr[i] = dic[key]
i += 1
print(arr)
輸出
[ 20. 2023. 10.]
使用numpy.fromiter()函式
在NumPy中,還有一個名為fromiter()的函式,用於將字典轉換為陣列。此函式採用字典值和資料型別作為輸入引數。以下是語法。
numpy.fromiter(dictionary,dtype = datatype)
示例
在這個示例中,我們將透過將字典作為輸入引數傳遞給fromiter()函式來將字典轉換為陣列。
import numpy as np
dic = {'Age': 20, 'year': 2023,'class':10}
print("The dictionary to be converted into array:",dic)
arr = np.fromiter(dic.values(),dtype = int)
print("The array created from the dictionary:",arr)
print(type(arr))
輸出
The dictionary to be converted into array: {'Age': 20, 'year': 2023, 'class': 10}
The array created from the dictionary: [ 20 2023 10]
<class 'numpy.ndarray'>
使用numpy.column_stack()函式
當我們想將字典轉換為多行陣列時,我們可以使用NumPy陣列的column_stack()函式。以下是語法。
numpy.column_stack(dictionary[key] for key in dictionary)
示例
在這裡,我們將字典作為輸入引數傳遞給column_stack()函式,然後字典將轉換為具有多行的陣列。
import numpy as np
dic = {'Age': [20,20], 'year': [2023,2022],'class':[10,9]}
print("The dictionary to be converted into array:",dic)
arr = np.column_stack(dic[key] for key in dic)
print("The array created from the dictionary:",arr)
print(type(arr))
輸出
The dictionary to be converted into array: {'Age': [20, 20], 'year': [2023, 2022], 'class': [10, 9]}
The array created from the dictionary: [[ 20 2023 10]
[ 20 2022 9]]
<class 'numpy.ndarray'>
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP