Best way to hash a dictionary

Miki Tebeka tebeka at cs.bgu.ac.il
Mon Mar 3 07:01:46 EST 2003


Hello Steve,

> You will, I hope, pardon me saying that it sounds like rather important
> design decisions have already been made. Perhaps if you were to explain
> *why* you need to use dictionary equivalents as dictionary keys someone
> might be able to spot a solution that doesn't require such contortions.
Yap. It design decision. I'm doing a NLP M.Sc. thesis where I tag
words with part of speech. I Hebrew words have complex part of speech
with many attributes.
An example might be: {'POS':'Verb', 'Gender':'Male', 'Tense':'Past'}
My algorithm is a learning one and I need to store some statistics per
tag, the most efficient way is hash tables.

> Please note, I'm not saying you definitely don't need to use dictionaries as
> dictionary keys. Simply that it sounds like there may be a better way.
If you find one, please tell me. Basically I need immutable hash.

> Finally: the reason you can't just use the dictionaries as keys, of course,
> is that dictionaries are mutable. Does you design preclude any change to the
> dicionaries after they become keys in the other dictionary? If so then you
> may just be able to subclass the standard dict type.
This has performance penalties, and since an average run of my thesis
is 5 days I can't do that. Plus if you try:
>>> class D(dict):
	pass

>>> d = D()
>>> h = {}
>>> h[d] = 1
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in ?
    h[d] = 1
TypeError: dict objects are unhashable
>>> 

Miki




More information about the Python-list mailing list