List or Dictionairy or neither?

Bengt Richter bokr at accessone.com
Thu Aug 23 14:13:32 EDT 2001


On Thu, 23 Aug 2001 09:44:45 +0200, Dietmar Lang <dietmar at wohnheim.fh-wedel.de> wrote:

>
>Hi!
>
>> One idea would be to write all the strings end to end into a file, and
>> store their start byte positions and lengths as tuple values for keys in a dictionary,
>> somethin like
>> [code snip]
>
>Hey, that sounds rather good to me. I'll think about the database, too.
>But somehow I like this 'homemade' approach a little better. ;-)
>
Your own idea of using constant-size string slots in a file would be the
fastest, since you wouldn't have to load a dictionary or list telling
where the strings are located (assuming you have sequential numeric keys).
You'd want to avoid having to read a large dictionary if your execution
context doesn't allow using it more than for one lookup. It's all is a matter
of balancing actual needs and constraints. 

Your layout also has the advantage that you could replace a particular
string with any changed string that would fit in the slot. In the
back-to-back format, space would be saved, but you would have to re-write
the whole thing, or append the change at the end and update the index,
to accomplish an edit.

If you find yourself writing your own hash of non-sequential keys to access
disk file slots etc., you probably ought to stop reinventing DB stuff, unless
you really enjoy it ;-) Note though that if someone else will inherit your code,
standard methods will make life easier for them. OTOH, small Python things
tend to be very understandable ;-)

Another consideration is to wrap up your Python for this lookup functionality
so that you could replace it with a C extension if necesssary/desirable.

Good luck.




More information about the Python-list mailing list