Re: Retrieving an object from a set

Discussion in 'Python' started by MRAB, Jan 26, 2013.

  1. MRAB

    MRAB Guest

    On 2013-01-26 00:26, Ian Kelly wrote:
    > On Fri, Jan 25, 2013 at 4:45 PM, MRAB <> wrote:
    >> 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

    >
    > But in practice he won't have y, only x. So that would have to be:
    >
    >>>> f = [m for m in S & set([x]) if m is x][0]
    >>>> f is y

    > False
    >
    > And it turns out that my earlier "intersection" suggestion fails for
    > the same reason.
    >

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

    OK, so...

    The members that don't equal x are S - {x}.

    Remove those and you get the members that _do_ equal x:

    >>> (S - (S - {x})).pop() is y

    True
    MRAB, Jan 26, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnaud Delobelle

    Retrieving an object from a set

    Arnaud Delobelle, Jan 25, 2013, in forum: Python
    Replies:
    1
    Views:
    110
    Steven D'Aprano
    Jan 26, 2013
  2. Ian Kelly

    Re: Retrieving an object from a set

    Ian Kelly, Jan 25, 2013, in forum: Python
    Replies:
    0
    Views:
    107
    Ian Kelly
    Jan 25, 2013
  3. Ian Kelly

    Re: Retrieving an object from a set

    Ian Kelly, Jan 25, 2013, in forum: Python
    Replies:
    0
    Views:
    99
    Ian Kelly
    Jan 25, 2013
  4. MRAB
    Replies:
    0
    Views:
    108
  5. Ethan Furman

    Re: Retrieving an object from a set

    Ethan Furman, Jan 25, 2013, in forum: Python
    Replies:
    0
    Views:
    108
    Ethan Furman
    Jan 25, 2013
Loading...

Share This Page