Skip to content Skip to sidebar Skip to footer

2d Dictionary With Many Keys That Will Return The Same Value

I want to make a 2d dictionary with multiple keys per value. I do not want to make a tuple a key. But rather make many keys that will return the same value. I know how to make a 2d

Solution 1:

Here is a possible solution:

from collections import Iterable

classAliasDefaultDict():
    def__init__(self, default_factory, initial=[]):
        self.aliases = {}
        self.data = {}
        self.factory = default_factory
        for aliases, value in initial:
            self[aliases] = value

    @staticmethoddefdistinguish_keys(key):
        ifisinstance(key, Iterable) andnotisinstance(key, str):
            returnset(key)
        else:
            return {key}

    def__getitem__(self, key):
        keys = self.distinguish_keys(key)
        if keys & self.aliases.keys():
            return self.data[self.aliases[keys.pop()]]
        else:
            value = self.factory()
            self[keys] = value
            return value

    def__setitem__(self, key, value):
        keys = self.distinguish_keys(key)
        if keys & self.aliases.keys():
            self.data[self.aliases[keys.pop()]] = value
        else:
            new_key = object()
            self.data[new_key] = value
            for key in keys:
                self.aliases[key] = new_key
            return value

    def__repr__(self):
        representation = defaultdict(list)
        for alias, value in self.aliases.items():
            representation[value].append(alias)
        return"AliasDefaultDict({}, {})".format(repr(self.factory), repr([(aliases, self.data[value]) for value, aliases in representation.items()]))

Which can be used like so:

>>> a_dict = AliasDefaultDict(dict)
>>> a_dict['food', 'canned_food']['spam'] = 'delicious'>>> a_dict['food']
{'spam': 'delicious'}
>>> a_dict['canned_food']
{'spam': 'delicious'}
>> a_dict
AliasDefaultDict(<class'dict'>, [(['food', 'canned_food'], {'spam': 'delicious'})])

Note there are some edge cases with undefined behavior - such as using the same key for multiple aliases. I feel this makes this data type pretty awful for general use, and I'd suggest that you may be better off changing your program not to need this kind of overly convoluted structure instead.

Also note this solution is for 3.x, under 2.x, you will want to swap out str for basestring, and self.aliases.keys() for self.aliases.viewkeys().

Solution 2:

Does this help at all?

classMultiDict(dict):
    # define __setitem__ to set multiple keys if the key is iterabledef__setitem__(self, key, value):
        try:
            # attempt to iterate though items in the keyfor val in key:
                dict.__setitem__(self, val, value)
        except:
            # not iterable (or some other error, but just a demo)# just set that keydict.__setitem__(self, key, value)



x = MultiDict()

x["a"]=10
x["b","c"] = 20print x

The output is

{'a': 10, 'c': 20, 'b': 20}

Post a Comment for "2d Dictionary With Many Keys That Will Return The Same Value"