Николина обнови решението на 18.04.2012 20:13 (преди над 12 години)
+import collections
+
+
+class BiDict(dict):
+
+ def __getkey__(self, value):
+
+ for item in self.items():
+ if item[1] == value:
+ return item[0]
+ raise KeyError
+
+ def __setitem__(self, key, value):
+
+ if not isinstance(value, collections.Hashable):
+ raise TypeError
+ if value in self.values():
+ old_key = self.__getkey__(value)
+ self.pop(old_key)
+ dict.__setitem__(self, key, value)
+
+ def BiDict(self, constr_arg):
+
+ d = dict(constr_arg)
+ for item in d.items():
+ self.__setitem__(item[0], item[1])
+
+ def inverse(self):
+
+ d = BiDict()
+ for item in self.items():
+ d[item[1]] = item[0]
+ self.clear()
+ self.update(d)
+ return self
+
+ def update(self, constr_arg):
+
+ d = dict(constr_arg)
+ for item in d.items():
+ self.__setitem__(item[0], item[1])