Case-insensitive dict, non-destructive, fast, anyone?
Raymond Hettinger
vze4rx4y at verizon.net
Fri Apr 1 13:52:00 EST 2005
[Ville Vainio]
> I need a dict (well, it would be optimal anyway) class that stores the
> keys as strings without coercing the case to upper or lower, but still
> provides fast lookup (i.e. uses hash table).
>>> class S(str):
def __hash__(self):
return hash(self.lower())
def __eq__(self, other):
return self.lower() == other.lower()
>>> d = {}
>>> d[S('ThE')] = 'quick'
>>> d[S('the')]
'quick'
>>> d
{'ThE': 'quick'}
Raymond Hettinger
More information about the Python-list
mailing list