Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '(1,': 0.09; 'subject:set': 0.09; 'def': 0.10; '(say': 0.16; 'cleaner': 0.16; 'illustration': 0.16; 'self.obj': 0.16; 'simplest': 0.16; 'subject:object': 0.16; 'x).': 0.16; 'solution.': 0.18; '>>>': 0.18; 'tuples': 0.22; "haven't": 0.23; "i've": 0.23; 'set.': 0.27; 'message- id:@mail.gmail.com': 0.27; 'actual': 0.28; '(my': 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; 'received:google.com': 0.34; 'false': 0.35; 'problem,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'received:209': 0.37; 'object': 0.38; 'to:addr:python.org': 0.39; 'think': 0.40; 'containing': 0.61; 'here': 0.65; 'of:': 0.65; 'dear': 0.66; 'to:name:python': 0.84; 'trick.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:to :content-type; bh=xzLprq1Rox+I9BOATcAuhqr8i/3JHHsaC64Q0KLT9hw=; b=D8478RiDvqElpMvLGqC88KNWnO7UgsE2i4HNgcJgVVEoJxio7+UmvgjUT6OMe8FvLW mn2OYaDUh//LLd5XUUrUSgV5qWD6A23eKyOXFTNRUqlme4/RXbOym3ggPutIBSmv3UOs cyl+vKW08TgIffeFJFRmXnnNS6lD4Pr0N16wXh90o1aurzmJx6e8WtNOn1MvJVOwtVEa +eUnUoskSiHeK02ou91n02eP6+ebA1r45rU/AVXQmoP+b6cc5oQ8vAiB9YPboOUL4hvT 8IPqAXiFC0SJsQQ/0x7587SHMxRIUhtVbUhrFYP2AI9sv9p23QZB+mfF6U6JYV+/41WL F3+Q== MIME-Version: 1.0 X-Received: by 10.152.133.130 with SMTP id pc2mr6450019lab.51.1359155681835; Fri, 25 Jan 2013 15:14:41 -0800 (PST) Date: Fri, 25 Jan 2013 23:14:41 +0000 Subject: Retrieving an object from a set From: Arnaud Delobelle To: Python Content-Type: text/plain; charset=UTF-8 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359155683 news.xs4all.nl 6866 [2001:888:2000:d::a6]:54677 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37697 Dear Pythoneers, 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? -- Arnaud