Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37697
| 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 | <arnodel@gmail.com> |
| 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 <arnodel@gmail.com> |
| To | Python <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1057.1359155683.2939.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Retrieving an object from a set Arnaud Delobelle <arnodel@gmail.com> - 2013-01-25 23:14 +0000 Re: Retrieving an object from a set Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-26 17:25 +1100
csiph-web