Dealing with multiple sets

Discussion in 'Python' started by John Henry, Oct 26, 2006.

  1. John Henry

    John Henry Guest

    Hi list,

    If I have a bunch of sets:

    a = set((1, 2, 3))
    b = set((2, 3))
    c = set((1, 3))
    .....

    What's the cleanest way to say:

    1) Give me a list of the items that are in all of the sets? (3 in the
    above example)
    2) Give me a list of the items that are not in all of the sets? (1,2 in
    the above example)

    Thanks,
     
    John Henry, Oct 26, 2006
    #1
    1. Advertising

  2. John Henry

    John Henry Guest

    Oops. Forgot to mention, I am still using 2.3.


    John Henry wrote:
    > Hi list,
    >
    > If I have a bunch of sets:
    >
    > a = set((1, 2, 3))
    > b = set((2, 3))
    > c = set((1, 3))
    > ....
    >
    > What's the cleanest way to say:
    >
    > 1) Give me a list of the items that are in all of the sets? (3 in the
    > above example)
    > 2) Give me a list of the items that are not in all of the sets? (1,2 in
    > the above example)
    >
    > Thanks,
     
    John Henry, Oct 26, 2006
    #2
    1. Advertising

  3. John Henry

    Tim Peters Guest

    [John Henry]
    > If I have a bunch of sets:
    >
    > a = set((1, 2, 3))
    > b = set((2, 3))
    > c = set((1, 3))
    > ....
    >
    > What's the cleanest way to say:
    >
    > 1) Give me a list of the items that are in all of the sets? (3 in the
    > above example)


    list(a & b & c)

    > 2) Give me a list of the items that are not in all of the sets? (1,2 in
    > the above example)


    list((a | b | c) - (a & b & c))
     
    Tim Peters, Oct 26, 2006
    #3
  4. John Henry

    John Henry Guest

    Aye!

    I did a:

    a and b and c

    Bonk!

    Thanks,


    Tim Peters wrote:
    > [John Henry]
    > > If I have a bunch of sets:
    > >
    > > a = set((1, 2, 3))
    > > b = set((2, 3))
    > > c = set((1, 3))
    > > ....
    > >
    > > What's the cleanest way to say:
    > >
    > > 1) Give me a list of the items that are in all of the sets? (3 in the
    > > above example)

    >
    > list(a & b & c)
    >
    > > 2) Give me a list of the items that are not in all of the sets? (1,2 in
    > > the above example)

    >
    > list((a | b | c) - (a & b & c))
     
    John Henry, Oct 26, 2006
    #4
  5. At Wednesday 25/10/2006 21:12, John Henry wrote:

    >Oops. Forgot to mention, I am still using 2.3.


    try: set
    except NameError: from sets import Set as set

    and the code will work almost exactly the same in 2.3/2.4

    > > 1) Give me a list of the items that are in all of the sets? (3 in the
    > > above example)


    a & b & c
    & is the intersection operator.

    > > 2) Give me a list of the items that are not in all of the sets? (1,2 in
    > > the above example)


    (a | b | c) - (a & b & c)
    (take the union of all items) except (the ones that are
    simultaneously in all sets)


    --
    Gabriel Genellina
    Softlab SRL

    __________________________________________________
    Correo Yahoo!
    Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
    ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
     
    Gabriel Genellina, Oct 26, 2006
    #5
  6. John Henry

    Brian Beck Guest

    John Henry wrote:
    > What's the cleanest way to say:
    >
    > 1) Give me a list of the items that are in all of the sets? (3 in the
    > above example)
    > 2) Give me a list of the items that are not in all of the sets? (1,2 in
    > the above example)
    >
    > Thanks,


    If you have an arbitrary list of sets, reduce comes in handy:

    See this recipe:
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/476215

    py> sets = [set((1, 2, 3)), set((2, 3)), set((1, 3))]
    py> reduce(set.intersection, sets)
    set([3])

    py> reduce(set.union, sets)
    set([1, 2, 3])

    py> reduce(set.union, sets) - reduce(set.intersection, sets)
    set([1, 2])

    --
    Brian Beck
    Adventurer of the First Order
     
    Brian Beck, Oct 26, 2006
    #6
  7. John Henry

    John Henry Guest

    Oh, great. Learn something new everyday.

    For this, what I did was to build up a string, and then use eval on the
    string. Very ugly.

    Now I can simply do a reduce.

    Thanks,



    Brian Beck wrote:
    > John Henry wrote:
    > > What's the cleanest way to say:
    > >
    > > 1) Give me a list of the items that are in all of the sets? (3 in the
    > > above example)
    > > 2) Give me a list of the items that are not in all of the sets? (1,2 in
    > > the above example)
    > >
    > > Thanks,

    >
    > If you have an arbitrary list of sets, reduce comes in handy:
    >
    > See this recipe:
    > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/476215
    >
    > py> sets = [set((1, 2, 3)), set((2, 3)), set((1, 3))]
    > py> reduce(set.intersection, sets)
    > set([3])
    >
    > py> reduce(set.union, sets)
    > set([1, 2, 3])
    >
    > py> reduce(set.union, sets) - reduce(set.intersection, sets)
    > set([1, 2])
    >
    > --
    > Brian Beck
    > Adventurer of the First Order
     
    John Henry, Oct 26, 2006
    #7
    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. Chris Barker
    Replies:
    3
    Views:
    312
    Graham Dumpleton
    Apr 22, 2004
  2. Rohan
    Replies:
    2
    Views:
    330
    Neil Cerutti
    Aug 7, 2007
  3. none
    Replies:
    2
    Views:
    372
    Öö Tiib
    May 4, 2010
  4. Isak
    Replies:
    2
    Views:
    83
  5. Bernie Cosell
    Replies:
    8
    Views:
    276
    Alan J. Flavell
    Oct 29, 2003
Loading...

Share This Page