将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