Никола обнови решението на 18.04.2012 16:38 (преди над 12 години)
+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ super(BiDict, self).__init__()
+ for argument in args:
+ for key, val in argument.items():
+ hash(key)
+ hash(val)
+ self[key] = val
+ for key, val in kwargs.items():
+ hash(key)
+ hash(val)
+ self[key] = val
+
+ def __setitem__(self, key, val):
+ hash(val)
+ hash(key)
+ if val in self.values():
+ for search_key, search_val in self.items():
+ if search_val == val:
+ key_to_remove = search_key
+ del self[key_to_remove]
+ super().__setitem__(key, val)
+
+ def inverse(self):
+ inverted = [{val:key} for key, val in self.items()]
+ self.clear()
+ for new_pair in inverted:
+ self.update(new_pair)
+
+ def update(self, dict):
+ for key, val in dict.items():
+ hash(key)
+ hash(val)
+ self[key] = val