Which is the best approach of this data structure in python?
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 conv开发者_如何学运维inient 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.
To "fill in the ontology dictionary with only the key names" :
ontology = {}.fromkeys('abcd',{})
or
ontology = dict( (k,{}) for k in 'abcd')
Then you will fill {} of each item of ontology with the items 'a':[...] , 'b':[...] , 'c':[...]
.
But I don't see the interest of writing the value ontology['a']['a'] as a list containing one unique element that is a dictionary with one unique item having always the same key 'y'
Instead of writing this value 'a':[{'y': {'type': 'DSE','name':'JHG'}}] , for example, you could write it 'a':('DSE','JHG') . Knowing that first element of the couple ('DSE','JHG') is a type and the second is a name.
You could also write 'z':('QSZA','IUYOIU') , knowing that first== a type and second== a name.
So ontology would be:
{'a': {'a':('DSE','JHG') , 'x':[...] , 'z':('QSZA','IUYOIU')} ,
'b': {'b':('dfg','tfy') , 'x':[...] , 'z':('ynyu','zertzt')} ,
'c': {'c':('noq','jek') , 'x':[...] , 'z':('frEZ','jkyukA')} ,
'd': {'d':('bqi','bif') , 'x':[...] , 'z':('kiy',';jheTri')} }
And it could even be simplified still more:
{'a': (('a', 'DSE','JHG') , [...] , ('QSZA','IUYOIU')) ,
'b': (('b', 'dfg','tfy') , [...] , ('ynyu','zertzt')) ,
'c': (('c', 'noq','jek') , [...] , ('frEZ','jkyukA')) ,
'd': (('d', 'bqi','bif') , [...] , ('kiy',';jheTri')) }
First element of a value of ontology would always be of style ('a', type, name) , the second always a list of style 'x' , and the third always a couple (type, name) of style 'z'
The elements ('a', type, name) , [...] , (type, name) would be accessed through positions 0, 1, 2 instead of keys 'a'+'y', 'x', 'z'
Why not use XML? ElementTree ships with Python and offers a lightweight XML API. The advantages are that you can easily create hierarchical data structures and all the tools to traverse/query your structure are there. This example creates a simple XML snippet,
from xml.etree import ElementTree
element = ElementTree.Element
xmlprint = ElementTree.tostring
o = element('ontology')
for key in ['a','b']:
o.append(element(key,name=key))
for c in o.getchildren():
c.append(element('type'))
print xmlprint(o)
Which gives,
<ontology><a name="a"><type /></a><b name="b"><type /></b></ontology>
精彩评论