Better dict of dicts

Bill Jackson jackson at hotmail.com
Thu Apr 19 17:24:37 EDT 2007


I have a dictionary of dictionaries where the keys are typically very 
long tuples and repeated in each inner dictionary.  The dictionary 
representation is nice because it handles sparseness well...and it is 
nice to be able to look up values based on a string rather than a 
number.  However, since my keys are quite long, I worry that I am 
wasting a lot of memory.  I'm looking for better data structures.

Here is an example:

 >>> a = {"string_1": {"string_2":1,
...                   "string_3":5,
...                   "string_4":10},
...      "string_2": {"string_2":12,
...                   "string_6":2,
...                   "string_1":4}}

So as my strings get longer and longer, it seems that the dictionary of 
dictionary representation is less and less efficient.

My thought was to subclass the dictionary structure....

keys = {"string_1":1,
         "string_2":2,
         "string_3":3,
         "string_4":4,
         "string_6":5}

Then the underlying dictionary of dictionaries would look like:

a = {1:{2:1,3:5,4:10},2:{2:12,5:2,1:4}}

Somehow I need to intercept every possible call though....such that

a["string_1"]["string_2"]   actually calls a[1][2]

and

a.keys()  returns ["string_1", "string_2", "string_3"....]
           rather than [1,2,3,4,5]

etc.

Ideally, I would like the option to have different key hashes for the 
rows and columns as well.

Any ideas?





More information about the Python-list mailing list