Python 如何将字典转换为矩阵或nArray
在本文中,我们将介绍如何使用Python的NumPy库中的array()函数将字典转换为矩阵或NumPy数组。
有时需要将Python中的字典转换为NumPy数组,并且Python提供了高效的方法来实现这一点。将字典转换为NumPy数组将得到一个包含字典中键值对的数组。
在本节中,我们将看到如何将不同类型的字典转换为Python中的NumPy数组的示例:
- 将字典转换为NumPy数组
- 将嵌套字典转换为NumPy数组
- 将具有混合键的字典转换为NumPy数组
numpy.array()函数
它返回一个ndarray。ndarray是满足给定要求的数组对象。
要将字典转换为NumPy数组,Python提供了numpy.array()方法,但我们必须先进行一些准备工作。按照以下三个基本步骤进行预处理:
- 首先,使用dict.items()获取字典中的键值对组。
- 然后,将这个组作为对象,使用list(obj)将其转换为列表。
- 最后,使用这个列表作为数据,调用numpy.array(data)将其转换为数组。
语法
numpy.array(object, dtype = None, *, copy = True, order = ‘K’, subok = False, ndmin = 0)
参数
- object - 这是一个数组或任何实现数组接口的对象。
-
dtype - 数组的首选数据类型。
-
copy - 如果为真(默认值),则将复制该项。否则,只有在array返回复制时才会产生一个副本。
-
order - 它表示数组的内存布局。
-
subok - 如果为真,则会传递子类;否则,返回的数组会强制成为一个基类数组(默认)。
-
ndmin - 表示结果数组的最小维数。
-
返回值 - 返回一个ndarray(它是满足指定要求的数组对象)。
将字典转换为NumPy数组
步骤
以下是执行所需任务的算法/步骤:
- 使用import关键字导入numpy模块,并使用np作为别名。
-
创建一个变量来存储输入字典。
-
对输入字典应用items()函数(返回字典中的键值对组)以获取字典中的所有键值对,并创建一个变量来存储它们。
-
使用list()函数(返回可迭代对象的列表)将字典的所有键值对转换为列表数据类型。
-
使用NumPy模块的array()函数(返回一个ndarray。ndarray是满足给定要求的数组对象)将上述数据列表转换为NumPy数组。
-
在转换后,打印输入字典的结果NumPy数组。
示例
下面的程序使用array()函数将输入字典转换为NumPy数组并返回:
# importing numpy module with an alias name
import numpy as np
# creating a dictionary
inputDict = {1: 'Hello',
2: 'Tutorialspoint',
3: 'python'}
# getting all the key-value pairs in the dictionary
result_keyvalpairs = inputDict.items()
# converting an object to a list
list_data = list(result_keyvalpairs)
# converting list to an numpy array using numpy array() function
numpy_array = np.array(list_data)
print("Input Dictionary =",inputDict)
# printing the resultant numpy array
print("The resultant numpy array:\n", numpy_array)
输出
执行上述程序后,将生成以下输出
Input Dictionary = {1: 'Hello', 2: 'Tutorialspoint', 3: 'python'}
The resultant numpy array:
[['1' 'Hello']
['2' 'Tutorialspoint']
['3' 'python']]
将嵌套字典转换为Numpy数组
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储输入 嵌套字典 (字典中包含另一个字典)。
-
使用 list() 函数(返回可迭代对象的列表)将字典的所有嵌套键值对转换为列表数据类型。
-
使用NumPy模块的 array() 函数将上述数据列表转换为NumPy数组。
-
打印转换后输入字典的结果NumPy数组。
示例
以下程序使用array()函数将嵌套输入字典转换为NumPy数组并返回它。
# importing NumPy module with an alias name
import numpy as np
# creating a nested dictionary
nestedDictionary = {1: 'Hello',
2: 'Tutorialspoint',
3: {'X': 'This is',
'Y': 'python',
'Z': 'code'}}
# getting all the key-value pairs in the dictionary
result_keyvalpairs = nestedDictionary.items()
# converting an object to a list
list_data = list(result_keyvalpairs)
# converting list to an array using numpy array() function
numpy_array = np.array(list_data)
print("Input nested Dictionary = ",nestedDictionary)
# printing the resultant numpy array
print("\nThe resultant numpy array:\n", numpy_array)
输出
执行上述程序后,将生成以下输出
Input nested Dictionary = {1: 'Hello', 2: 'Tutorialspoint', 3: {'X': 'This is', 'Y': 'python', 'Z': 'code'}}
The resultant numpy array:
[[1 'Hello']
[2 'Tutorialspoint']
[3 {'X': 'This is', 'Y': 'python', 'Z': 'code'}]]
将包含混合键的字典转换为NumPy数组
创建一个包含字符串、整数、浮点数、列表等混合键的输入字典,并用随机值填充它。
示例
以下程序使用array()函数将包含混合键的字典转换为NumPy数组,并返回它:
# importing numpy module with an alias name
import numpy as np
# creating a dictionary with mixed keys(like string and numbers as keys)
nestedDictionary = {'website': 'Tutorialspoint', 10: [2, 5, 8]}
# getting all the key-value pairs in the dictionary
result_keyvalpairs = nestedDictionary.items()
# converting an object to a list
list_data = list(result_keyvalpairs)
# converting list to an array using numpy array() function
numpy_array = np.array(list_data, dtype=object)
# printing the resultant numpy array
print("The resultant numpy array:\n", numpy_array)
输出
在执行上述程序时,将生成以下输出
The resultant numpy array:
[['website' 'Tutorialspoint']
[10 list([2, 5, 8])]]
结论
在本文中,我们了解了字典中各种类型的键值对以及如何在Python中将它们转换为矩阵或Numpy数组。