Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder2.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(1,': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:184.172': 0.09; 'received:gator410.hostgator.com': 0.09; 'solution,': 0.09; 'subject:set': 0.09; 'subtle': 0.09; '~ethan~': 0.09; 'def': 0.10; '(say': 0.16; 'cleaner': 0.16; 'illustration': 0.16; 'objects?': 0.16; 'self.obj': 0.16; 'simplest': 0.16; 'subject:object': 0.16; 'x).': 0.16; 'wrote:': 0.17; 'solution.': 0.18; 'tuples': 0.22; "haven't": 0.23; "i've": 0.23; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'set.': 0.27; 'actual': 0.28; '(my': 0.29; '>>>>': 0.29; 'obj': 0.29; 'seemingly': 0.29; 'objects': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; 'true.': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'equal': 0.33; 'false': 0.35; 'problem,': 0.35; 'pm,': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'charset:us-ascii': 0.36; 'two': 0.37; 'why': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'containing': 0.61; 'between': 0.63; 'here': 0.65; 'of:': 0.65; 'received:67.18': 0.65; 'us,': 0.74; 'trick.': 0.84; 'yours.': 0.93 Date: Fri, 25 Jan 2013 15:38:16 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Retrieving an object from a set References: In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: yes X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: ([173.12.184.235]) [173.12.184.235]:40906 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 1 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359157582 news.xs4all.nl 6895 [2001:888:2000:d::a6]:35566 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37701 On 01/25/2013 03:14 PM, Arnaud Delobelle wrote: > I've got a seemingly simple problem, but for which I cannot find a > simple solution. > > I have a set of objects (say S) containing an object which is equal to > a given object (say x). So > > x in S > > is true. So there is an object y in S which is equal to x. My > problem is how to retrieve y, without going through the whole set. > Here is a simple illustration with tuples (my actual scenario is not > with tuples but with a custom class): > >>>> y = (1, 2, 3) # This is the 'hidden object' >>>> S = set([y] + range(10000)) >>>> x = (1, 2, 3) >>>> x in S > True >>>> x is y > False > > I haven't found y. It's a very simple problem, and this is the > simplest solution I can think of: > > class FindEqual(object): > def __init__(self, obj): > self.obj = obj > def __hash__(self): > return hash(self.obj) > def __eq__(self, other): > equal = self.obj == other > if equal: > self.lastequal = other > return equal > >>>> yfinder = FindEqual(x) >>>> yfinder in S > True >>>> yfinder.lastequal is y > True > > I've found y! I'm not happy with this as it really is a trick. Is > there a cleaner solution? I don't know if there is a cleaner solution, and I quite like yours. Can you tell us, though, why you have to have y if x == y? Is there some subtle difference between the two equal objects? ~Ethan~