Matlab structure in Python [duplicate]

落花浮王杯 提交于 2020-01-03 05:34:26

问题


Is there any variable in python similar to Matlab structures?

I would like to create a structure within another structure as in Matlab, but in Python. I've looking into Python Dictionaries, but I have not find an easy way to access to its values. Which in Matlab is really easy.

In Matlab I would do the following:

Create the structure

structure.parent1.variable1 = 23;
structure.parent1.variable2 = 19;
structure.parent1.variable3 = 19;
structure.parent2.variable1 = 10;
structure.parent2.variable2 = 11;

structure = 

    parent1: [1x1 struct]
    parent2: [1x1 struct]

And then access to the variable simply tipying

structure.parent2.variable1

ans =

    10

回答1:


what is wrong with dictionaries in python - to create and then access them :

structure = {}
structure["parent1"] = {}
structure["parent1"]["variable1"] = 23;
structure["parent1"]["variable2"] = 19;
structure["parent1"]["variable3"] = 19;
structure["parent2"] = {}
structure["parent2"]["variable1"] = 10;
structure["parent2"]["variable2"] = 11;



回答2:


Using a dictionary you can access elements like this

structure['parent2']['variable1']


来源:https://stackoverflow.com/questions/23611197/matlab-structure-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!