Дамяна обнови решението на 18.04.2012 20:30 (преди над 12 години)
+import collections
+
+class BiDict(dict):
+ def BiDict(self, dictionary):
+ helper = dict(dictionary)
+ for (name, value) in helper.items():
+ self.__setitem__(name, value)
+
+ def inverse(self):
+ bdictionary = BiDict()
+ for (name, value) in self.items():
+ bdictionary.__setitem__(value, name)
+ self.clear()
+ self.update(bdictionary)
+
+ def getkey(self, value):
+ for (name, val) in self.items():
+ if val == value:
+ return name
+ raise KeyError
+
+ def __setitem__(self, name, value):
+ if not isinstance(value, collections.Hashable):
+ raise TypeError
+ else:
+ if value not in list(map(self.get, self.__iter__())):
+ dict.__setitem__(self, name, value)
+ else:
+ key = self.getkey(value)
+ self.pop(key)
+ dict.__setitem__(self, name, value)
+
+ def update(self, E):
+ helper = dict(E)
+ for (name, value) in helper.items():
+ self.__setitem__(name, value)