Re: Dictionary bidirectional

2008-07-14 Thread Kless
But in my dictionary both keys and values are unique. On Jul 14, 7:34 am, Dennis Lee Bieber [EMAIL PROTECTED] wrote: On Sun, 13 Jul 2008 16:21:11 -0700 (PDT), Kless [EMAIL PROTECTED] declaimed the following in comp.lang.python: I need a dictionary where get the result from a 'key' (on left),

Re: Dictionary bidirectional

2008-07-14 Thread Ken Starks
Dennis Lee Bieber wrote: On Sun, 13 Jul 2008 16:21:11 -0700 (PDT), Kless [EMAIL PROTECTED] declaimed the following in comp.lang.python: I need a dictionary where get the result from a 'key' (on left), but also from a 'value' (on right), how to get it? I know that dictionaries aren't

Re: Dictionary bidirectional

2008-07-14 Thread bearophileHUGS
Larry Bates: The only case where it would be faster would be if most of the keys were NOT in the dictionary (rather odd use case). Otherwise I believe you will find the first way quicker as the exceptions are infrequent. I have written a small benchmark: from random import shuffle def

Re: Dictionary bidirectional

2008-07-14 Thread Kless
Akathorn Greyhat sent me by email the next solution, which althought isn't generic it works well: - You could make your own class for that, maybe something like # class MyCoolDictionary(dict): def __init__(self, *args, **kargs): dict.__init__(self, *args,

Re: Dictionary bidirectional

2008-07-14 Thread Impotent Verse
If keys and values are unique you could do this... -- # Left : Right roman = { One : I, Two : II, Three : III, Four: IV, Five: V, Six : VI, Seven : VII, Eight : VIII,

Re: Dictionary bidirectional

2008-07-14 Thread Akathorn Greyhat
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 class MyCoolDictionary(dict): def __init__(self, *args, **kargs): dict.__init__(self, *args, **kargs) def

Re: Dictionary bidirectional

2008-07-14 Thread Gerry
If keys and values are unique, maybe just store both in the same dictionary: mydict[a] = b mydict[b] = a ... Gerry On Jul 14, 8:31 am, Impotent Verse [EMAIL PROTECTED] wrote: If keys and values are unique you could do this... -- #         Left      : Right roman =

Dictionary bidirectional

2008-07-13 Thread Kless
I need a dictionary where get the result from a 'key' (on left), but also from a 'value' (on right), how to get it? I know that dictionaries aren't bidirectional, but is there any way without use two dictionaries? Thanks in advance! -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary bidirectional

2008-07-13 Thread WDC
On Jul 13, 4:21 pm, Kless [EMAIL PROTECTED] wrote: I need a dictionary where get the result from a 'key' (on left), but also from a 'value' (on right), how to get it? I know that dictionaries aren't bidirectional, but is there any way without use two dictionaries? Thanks in advance! You

Re: Dictionary bidirectional

2008-07-13 Thread bukzor
On Jul 13, 4:21 pm, Kless [EMAIL PROTECTED] wrote: I need a dictionary where get the result from a 'key' (on left), but also from a 'value' (on right), how to get it? I know that dictionaries aren't bidirectional, but is there any way without use two dictionaries? Thanks in advance! You

Re: Dictionary bidirectional

2008-07-13 Thread bearophileHUGS
bukzor: You need to use two dictionaries. Here's a class that someone's written that wraps it up into a single dict-like object for you: http://www.faqts.com/knowledge_base/view.phtml/aid/4376 It contains code like: try: del self.data[item] except KeyError: pass Exceptions are useful

Re: Dictionary bidirectional

2008-07-13 Thread Larry Bates
[EMAIL PROTECTED] wrote: bukzor: You need to use two dictionaries. Here's a class that someone's written that wraps it up into a single dict-like object for you: http://www.faqts.com/knowledge_base/view.phtml/aid/4376 It contains code like: try: del self.data[item] except KeyError:

Re: Dictionary bidirectional

2008-07-13 Thread Akathorn Greyhat
-- Forwarded message -- From: Akathorn Greyhat [EMAIL PROTECTED] Date: 2008/7/14 Subject: Re: Dictionary bidirectional To: Kless [EMAIL PROTECTED] 2008/7/14 Kless [EMAIL PROTECTED]: I need a dictionary where get the result from a 'key' (on left), but also from a 'value

Re: Dictionary bidirectional

2008-07-13 Thread bukzor
On Jul 13, 6:53 pm, Larry Bates [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: bukzor: You need to use two dictionaries. Here's a class that someone's written that wraps it up into a single dict-like object for you: http://www.faqts.com/knowledge_base/view.phtml/aid/4376 It contains