您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在Python中将字典转换为矩阵或nArray?

2025/1/9 15:21:09发布29次查看
在本文中,我们将向您展示如何使用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 − 如果为true(默认值),则复制该项。否则,只有当__array__返回一个副本时才会产生副本
order − 它表示数组的内存布局
subok − 如果为true,则子类会被传递;否则,返回的数组会被强制转换为基类数组(默认)
ndmin − 指示结果数组的最小维数。
return value − 返回一个ndarray(它是一个满足指定要求的数组对象)
将字典转换为numpy数组算法(步骤)以下是执行所需任务的算法/步骤:
使用import关键字,导入具有别名(np)的numpy模块。
创建一个变量来存储输入的字典。
将 items() 函数(返回字典中的键值对组)应用于输入的字典,以获取字典中的所有键值对,并创建一个变量来存储它。
使用list()函数(返回一个可迭代对象的列表),将字典的所有键值对转换为列表数据类型。
使用numpy模块的array()函数(返回一个ndarray。ndarray是一个满足给定要求的数组对象),将上述数据列表转换为numpy数组。
将输入字典转换后的numpy数组打印出来。
example以下程序使用array()函数将输入的字典转换为numpy数组,并返回它 -
# importing numpy module with an alias nameimport numpy as np# creating a dictionaryinputdict = {1: 'hello',2: 'tutorialspoint',3: 'python'}# getting all the key-value pairs in the dictionaryresult_keyvalpairs = inputdict.items()# converting an object to a listlist_data = list(result_keyvalpairs)# converting list to an numpy array using numpy array() functionnumpy_array = np.array(list_data)print(input dictionary =,inputdict)# printing the resultant numpy arrayprint(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数组打印出来。
example以下程序使用array()函数将嵌套输入字典转换为numpy数组,并返回它
# importing numpy module with an alias nameimport numpy as np# creating a nested dictionarynesteddictionary = {1: 'hello', 2: 'tutorialspoint', 3: {'x': 'this is', 'y': 'python', 'z': 'code'}}# getting all the key-value pairs in the dictionaryresult_keyvalpairs = nesteddictionary.items()# converting an object to a listlist_data = list(result_keyvalpairs)# converting list to an array using numpy array() functionnumpy_array = np.array(list_data)print(input nested dictionary = ,nesteddictionary)# printing the resultant numpy arrayprint(\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数组创建一个输入字典,其中包含字符串、整数、浮点数、列表等混合键,并用随机值填充它。
example以下程序使用array()函数将具有混合键的字典转换为numpy数组,并返回它−
# importing numpy module with an alias nameimport 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 dictionaryresult_keyvalpairs = nesteddictionary.items()# converting an object to a listlist_data = list(result_keyvalpairs)# converting list to an array using numpy array() functionnumpy_array = np.array(list_data, dtype=object)# printing the resultant numpy arrayprint(the resultant numpy array:\n, numpy_array)
输出在执行时,上述程序将生成以下输出
the resultant numpy array: [['website' 'tutorialspoint'] [10 list([2, 5, 8])]]
结论在本文中,我们学习了字典中各种类型的键值对以及如何将它们转换为矩阵或numpy数组。
以上就是如何在python中将字典转换为矩阵或narray?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product