I have the following situation in python:
I am parsing an ontology and i want to keep track of some properties of the ontology and build a data structure with the below characteristics:
-there will be a single key to access each value
-the value would be another key-value data structure with the following 3 enties:
‘x’:[] a simple list
‘y’:[{‘name’:value,’type’:value}] a list containing specific dictioanry key-values
‘z’:[{‘name’:value,’type’:value}] a list containing specific dictionary key-values
According with the above the final data structure that i though was:
ontology={'': [{'y': {'type': '','name':''}}],'x':[],'z':[ {'type': '', 'name': ''}]}
And finally i want multiple entries of this dinctionary.I do not want to use a list because the index will be an integer and i’d like to index my data through a key.
I will fill this data structure inside 3 different for loops.
At the first loop i will fill in the ontology dictionary with only the key names..I thought something like :
ontology['a']={'a': [{'y': {'type': '','name':''}}],'x':[],'z':[ {'type': '', 'name': ''}]}
But is this something been doing in the ‘python’ standard way.Is there any other more convinient way of doing this because it seems to me somehow weird.
In the end i’ll have something like this:
ontology['a']={'a':[{'y': {'type': '','name':''}}],'x':[],'z':[ {'type': '', 'name': ''}]}
ontology['b']={'b':[{'y': {'type': '','name':''}}],'x':[],'z':[ {'type': '', 'name': ''}]}
ontology['c']={'c':[{'y': {'type': '','name':''}}],'x':[],'z':[ {'type': '', 'name': ''}]}
ontology['d']={'d':[{'y': {'type': '','name':''}}],'x':[],'z':[ {'type': '', 'name': ''}]}
At the second loop based on the keys i will fill the x :[] value,which is by itself another dictionary
And at the third for loop i will fill the y and z keys.
Is this a good approach for this data structure?I also thought of using Classes in order of my code to be more “structured” but i think i would use much more lines of code
“Lines of code” is probably not the best metric to optimize. This looks like a classic example of outgrowing a “dictionaries and lists” solution. I’ve been there many times. Use a class, it will let you write understandable maintainable code, with named methods for manipulating your data structure.
This will give you flexibility to hide and change the underlying storage without changing the semantics, or the caller’s view of the data.