This is a better version of the class that I sen't you, now it raises a KeyError when you try to insert a value that is already in<br><br>############################<br>class MyCoolDictionary(dict):<br> def __init__(self, *args, **kargs):<br>
dict.__init__(self, *args, **kargs)<br><br> def __setitem__(self, *args, **kargs):<br> for i in dictio.values():<br> if args[0]==i or args[1]==i:<br> raise KeyError, 'Value already exists'<br>
<br> dict.__setitem__(self, *args, **kargs)<br> <br> def __getitem__(self, item):<br> try:<br> return dict.__getitem__(self, item)<br><br> except KeyError:<br> keys=[]<br>
for i in dictio.keys():<br> if dictio[i]==item:<br> keys.append(i)<br> if not keys:<br> raise KeyError, 'Can\'t found key or value "' + str(item) + '"'<br>
return keys<br>############################<br><br>Feel free to change anything you want or need<br><br>Regards,<br>Akathorn Greyhat<br><br><br><div class="gmail_quote">2008/7/14 Kless <<a href="mailto:jonas.esp@googlemail.com">jonas.esp@googlemail.com</a>>:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I need a dictionary where get the result from a 'key' (on left), but<br>
also from a 'value' (on right), how to get it?<br>
<br>
I know that dictionaries aren't bidirectional, but is there any way<br>
without use two dictionaries?<br>
<br>
<br>
Thanks in advance!<br>
<font color="#888888">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br>