【发布时间】:2013-03-12 20:36:58
【问题描述】:
我有一本字典,需要将其转换为 NumPy 结构化数组。我正在使用 arcpy 函数NumPyArraytoTable,因此 NumPy 结构化数组是唯一可以使用的数据格式。
基于此线程:Writing to numpy array from dictionary 和此线程:How to convert Python dictionary object to numpy array
我试过了:
result = {0: 1.1181753789488595, 1: 0.5566080288678394, 2: 0.4718269778030734, 3: 0.48716683119447185, 4: 1.0, 5: 0.1395076201641266, 6: 0.20941558441558442}
names = ['id','data']
formats = ['f8','f8']
dtype = dict(names = names, formats=formats)
array=numpy.array([[key,val] for (key,val) in result.iteritems()],dtype)
但我不断收到expected a readable buffer object
下面的方法有效,但很愚蠢,显然不适用于真实数据。我知道有一个更优雅的方法,我就是想不通。
totable = numpy.array([[key,val] for (key,val) in result.iteritems()])
array=numpy.array([(totable[0,0],totable[0,1]),(totable[1,0],totable[1,1])],dtype)
【问题讨论】: