Галин обнови решението на 19.04.2012 10:44 (преди над 12 години)
+class BiDict(dict):
+
+ def __init__(self, *args, **kwargs):
+ self.update(*args, **kwargs)
+
+ def __setitem__(self, index, value):
+ if not value.__hash__:
+ raise TypeError("unhashable value:"+ str(value))
+ delete_key = None
+ for key,val in zip(self,self.values()):
+ if value == val:
+ delete_key = key
+ break
+ if(delete_key):
+ self.__delitem__(key)
+ super(BiDict,self).__setitem__( index, value )
+
+ def update(self, *args, **kwargs):
+ if len(args) > 1:
+ raise TypeError("update expected < 1 arg, got %d" % len(args))
+ other = dict(*args, **kwargs)
+ for key in other:
+ self[key] = other[key]
+
+ def inverse(self):
+ temp_dict={}
+ for key, val in zip(self,self.values()):
+ temp_dict[val] = key
+ self.clear()
+ for key,val in zip(temp_dict,temp_dict.values()):
+ self[key] = val