Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Retrieving an object from a set

Reply
Thread Tools

Re: Retrieving an object from a set

 
 
Ian Kelly
Guest
Posts: n/a
 
      01-25-2013
On Fri, Jan 25, 2013 at 4:30 PM, Ian Kelly <(E-Mail Removed)> wrote:
> On Fri, Jan 25, 2013 at 4:14 PM, Arnaud Delobelle <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving an object from a set Arnaud Delobelle Python 1 01-26-2013 06:25 AM
Re: Retrieving an object from a set Dave Angel Python 0 01-25-2013 11:56 PM
Re: Retrieving an object from a set MRAB Python 0 01-25-2013 11:45 PM
Re: Retrieving an object from a set Ethan Furman Python 0 01-25-2013 11:38 PM
Re: Retrieving an object from a set Ian Kelly Python 0 01-25-2013 11:30 PM



Advertisments