Efficient Data Structure Questions

David Goodger goodger at users.sourceforge.net
Sun Aug 25 19:33:17 CEST 2002


Keep it simple.  How many key1 strings will you have?  If it's just a few,
linear searches are no big deal; your second example is better.  If it's
many, use a dictionary.  Put the search terms as keys to the dictionary.  Do
you need to access the 'key2' and 'key3' data independently of 'key1'?  If
not, just store a tuples:

    FM.key1 = {}
    FM.key1['string1'] = (0, [1,2,3,4,5])
    FM.key1['string2'] = (1, [6,7,8,9,10])

If you do need to access them all separately, and you have a lot of data,
use your second example and keep an auxiliary string-to-index mapping:

> FM.keywords={}
> 
> FM.keywords['key1']=["string1", "string2", ... ]
> FM.keywords['key2']=[0, 1, ... ]
> FM.keywords['key3']=[[1.2.3.4.5],[6,7,8,9,10],... ]

    FM.key1aux = {'string1': 0, 'string2': 1}

Although programmatically, of course.

If that doesn't help, tell us more about the data set.  Are the keywords
static or dynamic?  etc.

-- 
David Goodger  <goodger at users.sourceforge.net>  Open-source projects:
  - Python Docutils: http://docutils.sourceforge.net/
    (includes reStructuredText: http://docutils.sf.net/rst.html)
  - The Go Tools Project: http://gotools.sourceforge.net/




More information about the Python-list mailing list