Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37699

Re: Retrieving an object from a set

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.036
X-Spam-Evidence '*H*': 0.93; '*S*': 0.00; 'testing,': 0.07; '(1,': 0.09; 'subject:set': 0.09; '(say': 0.16; 'subject:object': 0.16; 'x).': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'solution.': 0.18; '>>>': 0.18; "i've": 0.23; 'header:In-Reply-To:1': 0.25; 'set.': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; 'seemingly': 0.29; 'objects': 0.29; 'fri,': 0.30; 'could': 0.32; 'true.': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'equal': 0.33; 'received:google.com': 0.34; 'needed': 0.35; 'false': 0.35; 'problem,': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'containing': 0.61; 'dear': 0.66; '2013': 0.84; 'dict.': 0.84; 'to:name:python': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=i+DpWVcYUYrVgy0UcMg3rVKd2j9wveA0awq5AKf4tgc=; b=K7Fcdp8yyt4RglR3PdQM/P+kP/3A5VIIXIOiQQ/fAfqwKuLO6WJNdjdWO8fmmcPky1 P9E1Q/d3xDuMUW+Mp5NAQok/HWibJ0QHgTb4OnEpBlMX/yVjVHo3pqJH+azhWzqTEY4v +oBpBGaEhBD3a/nVbqNFbHQatZ5W+KysMRWrt94ZauNzdsTO5WZo37v9xVUIDGmxjl6Y Y+YufR0TGpR272Q/xh23UWlNZwudZ4CuqYNS1ufiGzicHV4uHbkC+TqFnrM59DjgrdST DhGrsxFKfcOSbQwHoCWqnkP6tkETaOSkfFKsOB/XKqNbANsBqAIM7QGXhtukX5AZV/p7 dDaw==
X-Received by 10.66.87.67 with SMTP id v3mr17211108paz.63.1359157052801; Fri, 25 Jan 2013 15:37:32 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CALwzidnB2ucHWPpj6LeY1FQDsZzJK7=NDWzKv2Osr+2t=Z5ZLw@mail.gmail.com>
References <CAJ6cK1b=F5JXL=FN0qouS-idbE8xnVURm0ZZrZ8-DW2xbLNErg@mail.gmail.com> <CALwzidnB2ucHWPpj6LeY1FQDsZzJK7=NDWzKv2Osr+2t=Z5ZLw@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 25 Jan 2013 16:37:02 -0700
Subject Re: Retrieving an object from a set
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.1059.1359157062.2939.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359157062 news.xs4all.nl 6894 [2001:888:2000:d::a6]:60565
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37699

Show key headers only | View raw


On Fri, Jan 25, 2013 at 4:30 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Fri, Jan 25, 2013 at 4:14 PM, Arnaud Delobelle <arnodel@gmail.com> wrote:
>> 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.
>
> You could use a dict.
>
>>>> y = (1, 2, 3)
>>>> S = {x: x for x in [y] + range(10000)}
>>>> x = (1, 2, 3)
>>>> x in S
> True
>>>> x is y
> False
>>>> S[x] is y
> True

Or you could use a set intersection:

>>> S = set([y] + list(range(10000)))
>>> S.intersection([x]).pop()
(1, 2, 3)

In my testing, the time needed for this is small and does not seem to
depend on the size of the set.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Retrieving an object from a set Ian Kelly <ian.g.kelly@gmail.com> - 2013-01-25 16:37 -0700

csiph-web