473,839 Members | 1,410 Online

# Dealing with multiple sets

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,

Oct 25 '06 #1
6 1864
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,
Oct 25 '06 #2
[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))
Oct 25 '06 #3
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))
Oct 26 '06 #4
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
Oct 26 '06 #5
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/Coo.../Recipe/476215

pysets = [set((1, 2, 3)), set((2, 3)), set((1, 3))]
pyreduce(set.in tersection, sets)
set([3])

pyreduce(set.un ion, sets)
set([1, 2, 3])

pyreduce(set.un ion, sets) - reduce(set.inte rsection, sets)
set([1, 2])

--
Brian Beck
Oct 26 '06 #6
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/Coo.../Recipe/476215

pysets = [set((1, 2, 3)), set((2, 3)), set((1, 3))]
pyreduce(set.in tersection, sets)
set([3])

pyreduce(set.un ion, sets)
set([1, 2, 3])

pyreduce(set.un ion, sets) - reduce(set.inte rsection, sets)
set([1, 2])

--
Brian Beck