Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Retrieving an object from a set (http://www.velocityreviews.com/forums/t956943-re-retrieving-an-object-from-a-set.html)

MRAB 01-25-2013 11:45 PM

Re: Retrieving an object from a set
 
On 2013-01-25 23:14, Arnaud Delobelle 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.
> 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
>

You could first limit the search to only those which it could be:

S & set([y])

A search would be:

>>> f = [m for m in S & set([y]) if m is y][0]
>>> f is y

True



All times are GMT. The time now is 01:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57