将Python字典列表映射到Matlab结构

将Python字典列表映射到Matlab结构,python,matlab,dictionary,struct,mapping,Python,Matlab,Dictionary,Struct,Mapping,我正在尝试将Python字典列表映射到特定的.matfromat。我之所以需要这样做,是因为我使用Python开发了一些代码来生成“绘画”(即由这样一个字典表示),但我想重用另一个项目提供的Matlab程序,该项目将这种结构的字典可视化 在整个项目中,我确保生成的所有绘画的名称、类型和结构都与Matlab项目的示例数据集相匹配。现在,最后一步是将其转换为此.matfromat。为此,我使用了numpy和scipy.io。然而,需要注意的是,我对Matlab没有任何经验,这就是我用Python开发

我正在尝试将Python字典列表映射到特定的
.mat
fromat。我之所以需要这样做,是因为我使用Python开发了一些代码来生成“绘画”(即由这样一个字典表示),但我想重用另一个项目提供的Matlab程序,该项目将这种结构的字典可视化

在整个项目中,我确保生成的所有绘画的名称、类型和结构都与Matlab项目的示例数据集相匹配。现在,最后一步是将其转换为此
.mat
fromat。为此,我使用了
numpy
scipy.io
。然而,需要注意的是,我对Matlab没有任何经验,这就是我用Python开发代码的原因

我的Python字典:

[{'xmax': 593, 'ymax': 685, 'h_pts': [0, 168, 362, 685], 'v_pts': [0, 25, 468, 487, 540, 593], 'h_thick': [0, 14, 16, 0], 'v_thick': [0, 14, 15, 16, 17, 0], 'h_ext': [[1, 6], [2, 6], [2, 5], [1, 6]], 'v_ext': [[1, 4], [1, 4], [2, 4], [2, 3], [2, 4], [1, 4]], 'rect': [[4, 3, 3, 5], [4, 3, 2, 3], [4, 2, 5, 6], [4, 1, 1, 2], [3, 2, 4, 5], [3, 2, 3, 4], [3, 2, 2, 3], [2, 1, 2, 6]], 'rect_colors': [2, 3, 1, 3, 3, 3, 1, 1]}]
saveformat = {}
paintings = [<python dictionary as above here>, ...]   
representations = np.asarray(representations).astype('object')    
saveformat['reps'] = representations
sio.savemat("cool_name.mat", saveformat)
文件
.mat
的示例表示形式(我需要):

我目前正在尝试的:

[{'xmax': 593, 'ymax': 685, 'h_pts': [0, 168, 362, 685], 'v_pts': [0, 25, 468, 487, 540, 593], 'h_thick': [0, 14, 16, 0], 'v_thick': [0, 14, 15, 16, 17, 0], 'h_ext': [[1, 6], [2, 6], [2, 5], [1, 6]], 'v_ext': [[1, 4], [1, 4], [2, 4], [2, 3], [2, 4], [1, 4]], 'rect': [[4, 3, 3, 5], [4, 3, 2, 3], [4, 2, 5, 6], [4, 1, 1, 2], [3, 2, 4, 5], [3, 2, 3, 4], [3, 2, 2, 3], [2, 1, 2, 6]], 'rect_colors': [2, 3, 1, 3, 3, 3, 1, 1]}]
saveformat = {}
paintings = [<python dictionary as above here>, ...]   
representations = np.asarray(representations).astype('object')    
saveformat['reps'] = representations
sio.savemat("cool_name.mat", saveformat)
saveformat={}
绘画=[,…]
representations=np.asarray(representations.astype('object'))
saveformat['reps']=表示法
sio.savemat(“cool_name.mat”,saveformat)
我在Matlab中的结果:

[{'xmax': 593, 'ymax': 685, 'h_pts': [0, 168, 362, 685], 'v_pts': [0, 25, 468, 487, 540, 593], 'h_thick': [0, 14, 16, 0], 'v_thick': [0, 14, 15, 16, 17, 0], 'h_ext': [[1, 6], [2, 6], [2, 5], [1, 6]], 'v_ext': [[1, 4], [1, 4], [2, 4], [2, 3], [2, 4], [1, 4]], 'rect': [[4, 3, 3, 5], [4, 3, 2, 3], [4, 2, 5, 6], [4, 1, 1, 2], [3, 2, 4, 5], [3, 2, 3, 4], [3, 2, 2, 3], [2, 1, 2, 6]], 'rect_colors': [2, 3, 1, 3, 3, 3, 1, 1]}]
saveformat = {}
paintings = [<python dictionary as above here>, ...]   
representations = np.asarray(representations).astype('object')    
saveformat['reps'] = representations
sio.savemat("cool_name.mat", saveformat)

单击上面的一个单元格时^

显然,这里出现了一些问题,我似乎无法从
scipy.io
的文档中准确地找出问题所在。它不是为字典的每个键值生成一列,然后为行中的每个字典实例列出这些键值,而是生成一行,每列中有一个字典(每幅画一个字典)


这样,问题是相当直接的,如何正确地映射这个格式?

也许这是次优的,但是,你有没有考虑过这样的熊猫行为?p>

dict1, dict2 = {'x': 1, 'y': 0, 'h': [1,2]}, {'x': -1, 'y':2, 'h':[3,3]}
df = pd.DataFrame(columns = dict1.keys(),data = [dict1,dict2])
你得到

 |-------|-------|-------|
 |   x   |   y   |   h   |
 |-------|-------|-------|
 |   1   |   0   | [1,2] |
 |-------|-------|-------|
 |   -1  |   2   | [3,3] |
 |-------|-------|-------|

也许这是次优的,但是你有没有考虑过像这样的熊猫?p>
dict1, dict2 = {'x': 1, 'y': 0, 'h': [1,2]}, {'x': -1, 'y':2, 'h':[3,3]}
df = pd.DataFrame(columns = dict1.keys(),data = [dict1,dict2])
你得到

 |-------|-------|-------|
 |   x   |   y   |   h   |
 |-------|-------|-------|
 |   1   |   0   | [1,2] |
 |-------|-------|-------|
 |   -1  |   2   | [3,3] |
 |-------|-------|-------|

我尝试过这个,但它似乎不起作用,导致了一个值错误。使用numpy和scipy.io是否没有好的方法来实现这一点?当您尝试生成数据帧或其他地方时,会出现值错误?它出现在这一行:
pd.dataframe(keys,paints)
,其中
keys=paints[1]。keys()
paints
是绘画列表(字典):
raise ValueError(f“传递值的形状为{传递},索引暗示{隐含}”)值错误:传递值的形状为(10,1),索引暗示(5,1)
dataframe调用中的第一个位置参数是数据,因此我将以kwargs的形式显式传递它们,即columns=keys,data=Paintings现在没有出现错误,但我得到的结果是:。当您单击结构时,那里没有数据。我尝试了这一方法,但它似乎无法产生值错误。是否有好的方法来解决此问题使用numpy和scipy.io执行此操作?当您尝试生成数据帧或其他位置时出现值错误?它出现在这一行:
pd.dataframe(keys,paints)
with
keys=paints[1]。keys()
paints
作为绘画列表(字典):
raisevalueerror(f“传递值的形状为{passed},index implied{implied}”)ValueError:传递值的形状是(10,1),index implied(5,1)
dataframe调用中的第一个位置参数是数据,因此我会显式地将它们作为kwargs传递,即columns=keys,data=paintings现在没有出现错误,但结果是:。单击结构时,那里没有数据。