Better dict of dicts

Bill Jackson jackson at hotmail.com
Fri Apr 20 01:11:26 CEST 2007


Martin v. Löwis wrote the following on 04/19/2007 02:43 PM:
> Bill Jackson schrieb:
>> I have a dictionary of dictionaries where the keys are typically very
>> long tuples and repeated in each inner dictionary. 
> 
> What I don't understand here: you say the keys are tuples, yet later,
> you show that the keys are strings. Which one is it?

Sorry, I was just lazy.  The keys will always be tuples...tuple of 
strings, tuples of numbers, tuples of objects....simply tuples.

> Instead of doing that, I would use a procedure called "interning".
> You may want to use the builtin intern function, or your can
> come up with your own interning:
> 
> interns = {}
> def local_intern(s):
>   return interns.setdefault(s, s)
> 
> Then, instead of
> 
> a[k1][k2] = value
> 
> do
> 
> a[local_intern(k1)][local_intern(k2)] = value
> 
> then all strings occur only once as keys, as long as the interns
> dictionary isn't cleared.
> 

So, my structure is something like this:

a = {tuple_1: {tuple_2:value_1, tuple_3:value_2},
      tuple_4: {tuple_1:value_3, tuple_3:value_4}
      tuple_5: {tuple_2:value_5, tuple_3:value_6, tuple_4:value_7}}

Since the tuples from the inner dictionaries and the outer dictionaries 
are frequently the same, I would benefit from using a single intern 
function. Then, the tuples will always be "pointing" to the values 
stored in the intern dictionary.

Now suppose there is little overlap between the keys for the outer 
dictionary and the inner dictionaries...but still much overlap between 
the various inner dictionaries.  Then, there is no point in using an 
intern function for the outer dictionary, but still a benefit for the 
inner dictionary.  Thus, something like the following would be appropriate:

a[k1][local_intern(k2)] = value

Have I understood this properly?



More information about the Python-list mailing list