Марин обнови решението на 19.04.2012 13:05 (преди над 12 години)
+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ super(BiDict, self).__init__()
+ for key, value in kwargs.items():
+ hash(key)
+ hash(value)
+ self[key] = value
+ for object in args:
+ for key, value in object.items():
+ hash(key)
+ hash(value)
+ self[key] = value
+
+ def __setitem__(self, key, value):
+ hash(value)
+ hash(key)
+ if value in self.values():
+ for current_key, current_value in self.items():
+ if self[current_key] == value:
+ delete_key = current_key
+ del self[delete_key]
+ super().__setitem__(key, value)
+
+ def inverse(self):
+ inverse_dict = {value: key for key, value in self.items()}
+ self.clear()
+ self.update(inverse_dict)
+
+ def update(self, update_dict):
+ for key, value in update_dict.items():
+ hash(key)
+ hash(value)
+ self[key] = value