将Python字典转换为NumPy数组的不同方法
在这篇文章中,我们将看到使用NumPy库将Python字典转换成Numpy数组的不同方法。有时需要将Python中的字典转换为NumPy数组,Python提供了一种有效的方法来执行这一操作。将一个字典转换为NumPy数组的结果是一个存放字典的键值对的数组。
让我们看看不同的方法。
方法1:使用numpy.array()和List Comprehension一起。
语法:  numpy.array( object,dtype = None,*,copy = True,order = ‘K’,subok = False,ndmin = 0 )
返回:一个满足指定要求的数组对象。
我们使用 np.array() 将一个 dictionary 转换为数组。为了将字典中的每一个值作为一个列表输入到 np.array() 中,我们使用了 List comprehension 的概念。
示例:
# importing required librariess
import numpy as np
from ast import literal_eval
  
# creating class of string
name_list = """{
   "column0": {"First_Name": "Akash",
   "Second_Name": "kumar", "Interest": "Coding"},
                  
   "column1": {"First_Name": "Ayush",
   "Second_Name": "Sharma", "Interest": "Cricket"},
     
   "column2": {"First_Name": "Diksha",
   "Second_Name": "Sharma","Interest": "Reading"},
     
   "column3": {"First_Name":" Priyanka",
   "Second_Name": "Kumari", "Interest": "Dancing"}
     
  }"""
print("Type of name_list created:\n",
      type(name_list))
  
# converting string type to dictionary
t = literal_eval(name_list)
  
# printing the original dictionary
print("\nPrinting the original Name_list dictionary:\n",
      t)
  
print("Type of original dictionary:\n",
      type(t))
  
# converting dictionary to numpy array
result_nparra = np.array([[v[j] for j in ['First_Name', 'Second_Name',
                                          'Interest']] for k, v in t.items()])
  
print("\nConverted ndarray from the Original dictionary:\n",
      result_nparra)
  
# printing the type of converted array
print("Type:\n", type(result_nparra))
输出:
Type of name_list created:   
<class ‘str’>
Printing the original Name_list dictionary:   
{‘column0’: {‘First_Name’: ‘Akash’, ‘Second_Name’: ‘kumar’, ‘Interest’: ‘Coding’},   
‘column1’: {‘First_Name’: ‘Ayush’, ‘Second_Name’: ‘Sharma’, ‘Interest’: ‘Cricket’},   
‘column2’: {‘First_Name’: ‘Diksha’, ‘Second_Name’: ‘Sharma’, ‘Interest’: ‘Reading’},   
‘column3’: {‘First_Name’: ‘ Priyanka’, ‘Second_Name’: ‘Kumari’, ‘Interest’: ‘Dancing’}}   
Type of original dictionary:   
<class ‘dict’>
Converted ndarray from the Original dictionary:   
[[‘Akash’ ‘kumar’ ‘Coding’]   
[‘Ayush’ ‘Sharma’ ‘Cricket’]   
[‘Diksha’ ‘Sharma’ ‘Reading’]   
[‘ Priyanka’ ‘Kumari’ ‘Dancing’]]   
Type:   
<class ‘numpy.ndarray’>
方法2:使用numpy.array()和dictionary_obj.items().
我们使用 np.array() 将 dictionary 转换为数组。为了获得字典中的每一个值,作为 np.array() 方法的输入列表,我们使用了 dictionary_obj.items()。
示例:
# importing library
import numpy as np
  
# creating dictionary as key as 
# a number and value as its cube
dict_created = {0: 0, 1: 1, 2: 8, 3: 27,
                4: 64, 5: 125, 6: 216}
  
# printing type of dictionary created
print(type(dict_created))
  
# converting dictionary to 
# numpy array 
res_array = np.array(list(dict_created.items()))
  
# printing the converted array
print(res_array)
  
# printing type of converted array
print(type(res_array))
输出:
<class 'dict'>
[[  0   0]
[  1   1]
[  2   8]
[  3  27]
[  4  64]
[  5 125]
[  6 216]]
<class 'numpy.ndarray'>
极客教程