Multikey Dict?
Sam Pointon
free.condiments at gmail.com
Sat Nov 12 19:29:21 EST 2005
> If I could just say to Python: john and graham (and ...) are all a part
> of a "superdict" and either their id or their name can be used as keys.
> Can I do that somehow?
Sure you can. There are two obvious ways to do this - enlist the aid of
a Superdict (or similar) class to take care of the details. A simple
implementation would be:
class Superdict(object):
def __init__(self, by_id, by_name):
self.by_id = by_id
self.by_name = by_name
def __getitem__(self, value):
if value.isdigit():
return self.by_id[value]
else:
return self.by_name[value]
The alternative is to use some sort of database for storing these
values. It doesn't really matter which (hell, you could even reinvent a
relational wheel pretty simply in Python), as any DB worth its salt
will do exactly what you need.
Hope that helps,
-Sam
More information about the Python-list
mailing list