Hendrik van Rooyen wrote: > > "7stud" <[EMAIL PROTECTED]> wrote: > > >>.......... But using a tuple as a >>key in a dictionary is probably something you will never do. > > > Yikes! I do this all the time... > > Think of an address in any one town. > It has a Street, and a number > (could be more complex, like a sub number for an apartment > in a block, but lets ignore that for now) > > so: > addy = ("First Avenue", 10) > Residents = {addy:"Frank Everyman",......} > > To make a thing where you can look up who lives at a place... > (this is simplistic of course, for illustrative purposes - few people > live completely alone. - it should be at least a list, or possibly a dict > by surname...) > > I find that when I do this, I almost invariably also create the > inverse dict: > > Addresses = {"Frank Everyman":addy,.....} > > So that you can get at stuff from all directions. If you keep it > up you have a home made relational database if you are not careful... > > But if you limit it to one thing and its inverse, its quite useful, and it > would be nice to have one "doubledict" that can be accessed as speedily > from either end... > > Sort of an internally linked list of mixed hashed immutables, where: > > doubledict["Frank Everyman"] yields addy, and > doubledict[addy] yields "Frank Everyman" > > It would have direct applicability in things like the label table in > an assembler, for debugging and so on. > > - Hendrik >
I'm thinking that, to avoid big a lot of ambiguity, such a double dict would need to be implemented as two distinctly referenced data structures: class DD(object): def __init__(self, adict): self._fwd = {} self._fwd.update(adict) self._bkwk = dict(v,k for k,v in adict.items()) def fwd(self, k) return self._fwd[k] def bkwd(self, k) return self._bkwd[k] def __setitem__(self, k, v): self._fwd[k] = v self._bkwd[v] = k def __getitem__(self, k): if (k in self._fwd) and (k in self._bkwd): raise KeyError, 'Which do I look in first?' else: raise KeyError, 'Too much guesswork. Use fwd() or bkwd().' James -- http://mail.python.org/mailman/listinfo/python-list