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


Groups > comp.lang.python > #37716

Re: Retrieving an object from a set

From Peter Otten <__peter__@web.de>
Subject Re: Retrieving an object from a set
Date 2013-01-26 09:10 +0100
Organization None
References <CAJ6cK1b=F5JXL=FN0qouS-idbE8xnVURm0ZZrZ8-DW2xbLNErg@mail.gmail.com> <51031922.2020104@mrabarnett.plus.com> <CALwzid=YsWTow=0E=_spexS1JFagPtPgBUi-ksDCfByLE8Kc7A@mail.gmail.com> <510345FA.5000607@mrabarnett.plus.com> <ke0265$qdh$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.1070.1359187813.2939.python-list@python.org> (permalink)

Show all headers | View raw


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
> Type "help", "copyright", "credits" or "license" for more information.
>>>> 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.

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


Thread

Re: Retrieving an object from a set Peter Otten <__peter__@web.de> - 2013-01-26 09:10 +0100

csiph-web