Борис обнови решението на 18.04.2012 02:08 (преди над 12 години)
+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ super(BiDict, self).__init__()
+ for element in args:
+ for key, value in element.items():
+ hash(key)
+ hash(value)
+ self[key] = value
+ for key, value in kwargs.items():
+ hash(key)
+ hash(value)
+ self[key] = value
+
+ def __setitem__(self, key, value):
+ hash(value)
+ hash(key)
+ same_values = False
+ for self_key, self_value in self.items():
+ if self[self_key] == value:
+ same_values = True
+ del_key = self_key
+ if same_values == True:
+ del self[del_key]
+ super().__setitem__(key, value)
+
+ def update(self, new_dict):
+ for key, value in new_dict.items():
+ hash(key)
+ hash(value)
+ self[key] = value
+
+ def inverse(self):
+ stack = []
+ cur_dict = {}
+ for key, value in self.items():
+ cur_dict = {value: key}
+ stack.append(cur_dict)
+ self.clear()
+ for inv_dict in stack:
+ self.update(inv_dict)