小编Chr*_*sta的帖子

python dict到numpy结构化数组

我有一个字典,我需要转换为NumPy结构化数组.我正在使用arcpy函数NumPyArraytoTable,因此NumPy结构化数组是唯一可用的数据格式.

基于这个线程:从字典和这个线程写入numpy数组:如何将Python字典对象转换为numpy数组

我试过这个:

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)
Run Code Online (Sandbox Code Playgroud)

但我一直在努力 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)
Run Code Online (Sandbox Code Playgroud)

python numpy arcpy

33
推荐指数
2
解决办法
12万
查看次数

标签 统计

arcpy ×1

numpy ×1

python ×1