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/t956958-re-retrieving-an-object-from-a-set.html)

 Peter Otten 01-26-2013 08:10 AM

Re: Retrieving an object from a set

Vito De Tullio wrote:

> MRAB wrote:
>
>> It turns out that both S & {x} and {x} & S return {x}, not {y}.

>
> curious.
>
> \$ python
> Python 2.7.3 (default, Jul 3 2012, 19:58:39)
> [GCC 4.7.1] on linux2
>>>> x = (1,2,3)
>>>> y = (1,2,3)
>>>> s = set([y])
>>>> (s & set([x])).pop() is y

> False
>>>> (set([x]) & s).pop() is y

> True
>
> maybe it's implementation-defined?

I think the algorithm looks for the smaller set:

>>> set(range(5)) & set(map(float, range(10)))

set([0, 1, 2, 3, 4])
>>> set(range(10)) & set(map(float, range(5)))

set([0.0, 1.0, 2.0, 3.0, 4.0])

You have two sets of the same length, so there is no advantage in swapping
them before calculating the intersection.

 All times are GMT. The time now is 12:41 AM.