Regular expression as dictionary key?
Daniel Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Dec 3 03:37:43 EST 2001
Luke <LLoeffler at home.com> wrote:
: Let's say you have a dict of words like
: words = {"dogs":1,"cats":2}
: Let's say I'm given a singular form of a word and want to match it with
: plurals still, but words["dog"] will obviously raise a KeyError.
: Without doing a linear substring search on every element in words.key(),
: is there a way to take advantage of the dict's binary tree layout
: properties (e.g. speed) like:
: words["dog*"] # where dog* is a Regex
: Nonexistant syntax, I know, but you get the idea... I smell a PEP
Hmmm... Interesting! Let's see:
###
import re
from UserDict import UserDict
from types import StringType
class RegexDict(UserDict):
"""A dictionary that supports regular expressions to get at
a key. Response to a post on c.l.py."""
def __getitem__(self, key):
if self.data.has_key(key): return self.data[key]
regex = key
if isinstance(key, StringType):
regex = re.compile(key)
for k in self.data.keys():
if regex.search(k):
return self.data[k]
raise KeyError, key
###
Let's see if this works:
###
>>> words = RegexDict({'dogs' : 1, "cats" : 2})
>>> words['cat'] ## Normal lookups work
2
>>> words['dog*'] ## And so do regular expressions!
1
>>> words['dog*r'] ## But we can still get KeyErrors.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/tmp/python-1176RQL", line 17, in __getitem__
KeyError: dog*r
###
Hope this helps!
More information about the Python-list
mailing list