473,320 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1816
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.intersection, sets)
set([3])

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

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

--
Brian Beck
Adventurer of the First Order
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.intersection, sets)
set([3])

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

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

--
Brian Beck
Adventurer of the First Order
Oct 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Thom McGrath | last post by:
I have a text area that people should type in (duh) which will later be displayed for other users via HTML. I've taken care of the HTML aspect in a pretty cool way, but I worry about character sets...
44
by: flyingfred0 | last post by:
A small software team (developers, leads and even the manager when he's had time) has been using (wx)Python/PostgreSQL for over 2 years and developed a successful 1.0 release of a client/server...
1
by: Ryan R. Rosario | last post by:
Hello - I am working on a scheduling application that has many "rules" for scheduling people. I throw each person into the set that corresponds to 2 teams. Then I split this large group (of...
2
by: Darko Jovisic | last post by:
Hi! Another silly question: If a stored procedure returns multiple result sets, how do I choose the one I want to insert into a table? For example sp_spaceused returns two result sets if...
4
by: randy.p.ho | last post by:
Using JDBC, is there a way to call a stored procedure with multiple return values? Thanks.
5
by: Stanley Sinclair | last post by:
I have a need to return multiple result sets from a stored procedure. Want that SP to call others to get the data. Win2003, db2 8.1.5. Can't figure out how to handle open cursors, and return...
6
by: mark | last post by:
I have an asp.net ecommerce web application on a remote web server. I'm using an Access database on the back end. I've notice a few strange things. When I mimic an multiple user environment by...
1
by: randall g | last post by:
I have a stored procedure which returns multiple result sets, enclosing each in its own tag. This works in ADO but not ADO.NET, where an error is returned by ExecuteXmlReader: "Invalid command...
12
by: Scott | last post by:
Front-end Access 2000 I have a stored procedure that has 2 parameters BusinessUnitID and Year. It returns multiple record sets (5 to be exact). I thought I could use a Pass through query but...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.