
在本文中,我们将向您展示如何使用Python的NumPy库中的array()函数将字典转换为矩阵或NumPy数组。
有时候需要将Python中的字典转换为NumPy数组,Python提供了一种高效的方法来实现这一点。将字典转换为NumPy数组会得到一个包含字典中键值对的数组。
在这个部分,我们将看一些在Python中将各种类型的字典转换为NumPy数组的示例
它返回一个 ndarray。ndarray 是一个满足给定要求的数组对象。
立即学习“Python免费学习笔记(深入)”;
要将字典转换为NumPy数组,Python提供了numpy.array()方法,但我们必须先进行一些准备工作。按照以下三个基本步骤作为前期任务。
numpy.array(object, dtype = None, *, copy = True, order = ‘K’, subok = False, ndmin = 0)
object − 这是一个数组或任何暴露数组接口的对象。
dtype − 数组的首选数据类型。
copy − 如果为true(默认值),则复制该项。否则,只有当__array__返回一个副本时才会产生副本
order − 它表示数组的内存布局
subok − 如果为true,则子类会被传递;否则,返回的数组会被强制转换为基类数组(默认)
ndmin − 指示结果数组的最小维数。
Return Value − 返回一个ndarray(它是一个满足指定要求的数组对象)
以下是执行所需任务的算法/步骤:
使用import关键字,导入具有别名(np)的numpy模块。
创建一个变量来存储输入的字典。
将 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']]
以下是执行所需任务的算法/步骤:
创建一个变量来存储一个输入的嵌套字典(一个字典中包含另一个字典)。
使用 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'}]]
创建一个输入字典,其中包含字符串、整数、浮点数、列表等混合键,并用随机值填充它。
以下程序使用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])]]
在本文中,我们学习了字典中各种类型的键值对以及如何将它们转换为矩阵或Numpy数组。
以上就是如何在Python中将字典转换为矩阵或nArray?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号