Мирослав обнови решението на 16.04.2012 00:05 (преди над 12 години)
+class BiDict(dict):
+ def __init__(self, _dict):
+ for value in _dict.values():
+ hash(value)
+ dict.__init__(self)
+ #duplicate check not necessary
+ self.update(_dict)
+
+ def _remove_dups(self):
+ for i in range(2):
+ self.inverse()
+
+ def inverse(self):
+ inversed_dict = {v: k for k, v in self.items()}
+ self.clear()
+ dict.update(self, inversed_dict)
+
+ def __setitem__(self, key, value):
+ hash(value)
+ dict.update(self, {key: value})
+ self._remove_dups()
+
+ @classmethod
+ def fromkeys(cls, keys, value=None):
+ hash(value)
+ return BiDict(dict.fromkeys(keys, value))
+
+ def setdefault(self, key, default=None):
+ hash(default)
+ if key in self.keys():
+ return self[key]
+ else:
+ self[key] = default
+ self_remove_dups()
+ return default
+
+ def update(self, _dict):
+ for value in _dict.values():
+ hash(value)
+ dict.update(self, _dict)
+ self._remove_dups()
+
+ def copy(self):
+ return BiDict(dict.copy(self))