473,385 Members | 1,912 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,385 software developers and data experts.

sets and subsets

By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:

a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
c = [2,4] # A sub-set

I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:

a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]

How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.

Jul 18 '05 #1
16 1727
I am sure there is a much more elegant way to do this, but here is one
solution.

for item in c:
if b.count(item) > 0:
b.remove(item)
"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:

a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
c = [2,4] # A sub-set

I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:

a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]

How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.

Jul 18 '05 #2
b=[x for x in b if x not in c]

"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
| By using lists, I can create sets of number. Suppose I have three lists.
| One list is the super-set, one is a set that contains all the numbers
| (just like the super-set) and the last is sub-set of the super-set. For
| example:
|
| a = [1,2,3,4,5] # The super-set.
| b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
| c = [2,4] # A sub-set
|
| I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
| this would make the sets look like this:
|
| a = [1,2,3,4,5]
| b = [1,3,5]
| c = [2,4]
|
| How do I test set c to find what it contains and then look at set b to
| see if it contains any of those same numbers, and if so, remove them.
|
Jul 18 '05 #3
There you go... list comprehension. That is definatly nicer to look at.
"Elaine Jackson" <el***************@home.com> wrote in message
news:zcxWb.464065$JQ1.270296@pd7tw1no...
b=[x for x in b if x not in c]

"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
| By using lists, I can create sets of number. Suppose I have three lists.
| One list is the super-set, one is a set that contains all the numbers
| (just like the super-set) and the last is sub-set of the super-set. For
| example:
|
| a = [1,2,3,4,5] # The super-set.
| b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
| c = [2,4] # A sub-set
|
| I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
| this would make the sets look like this:
|
| a = [1,2,3,4,5]
| b = [1,3,5]
| c = [2,4]
|
| How do I test set c to find what it contains and then look at set b to
| see if it contains any of those same numbers, and if so, remove them.
|

Jul 18 '05 #4
On 2004-02-11, Bart Nessux <ba*********@hotmail.com> wrote:
By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:

a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
c = [2,4] # A sub-set

I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:

a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]

How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.


from sets import Set
a = Set([1, 2, 3, 4, 5])
b = Set([1, 2, 3, 4, 5])
c = Set([2, 4])
s = b - c
s

Set([1, 3, 5])

Jul 18 '05 #5
Works great too. Thanks to all for the info.

Amy G wrote:
There you go... list comprehension. That is definatly nicer to look at.
"Elaine Jackson" <el***************@home.com> wrote in message
news:zcxWb.464065$JQ1.270296@pd7tw1no...
b=[x for x in b if x not in c]

"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
| By using lists, I can create sets of number. Suppose I have three lists.
| One list is the super-set, one is a set that contains all the numbers
| (just like the super-set) and the last is sub-set of the super-set. For
| example:
|
| a = [1,2,3,4,5] # The super-set.
| b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
| c = [2,4] # A sub-set
|
| I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
| this would make the sets look like this:
|
| a = [1,2,3,4,5]
| b = [1,3,5]
| c = [2,4]
|
| How do I test set c to find what it contains and then look at set b to
| see if it contains any of those same numbers, and if so, remove them.
|



Jul 18 '05 #6
Bart Nessux wrote:
By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:

a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
c = [2,4] # A sub-set

I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:

a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]

How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.


You want set operations, so why would you use lists?
from sets import Set
a = Set([1,2,3,4,5])
c = Set([2,4])
b = a - c
b

Set([1, 3, 5])

Peter
Jul 18 '05 #7
Peter Otten wrote:
Bart Nessux wrote:

By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:

a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
c = [2,4] # A sub-set

I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:

a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]

How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.

You want set operations, so why would you use lists?


All my data are in lists:

inputFile = file('ips.txt', 'r') #Super-set
include = inputFile.readlines()
inputFile.close()

# The file below is compiled manually by hand... add IPs to it
# whenever you want to exclude them from IP_protection.
readFile = file('excluded_ips.txt', 'r') #Sub-set to exclude
exclude = readFile.readlines()
readFile.close()

include = [x for x in include if x not in exclude] #Magic of Elaine

outputFile = file('pruned_ips.txt' , 'w')
for i in include:
print>> outputFile, i,
outputFile.close()


Jul 18 '05 #8
[content at *bottom*]

In article <c0**********@solaris.cc.vt.edu>,
Bart Nessux <ba*********@hotmail.com> wrote:
Works great too. Thanks to all for the info.

Amy G wrote:
There you go... list comprehension. That is definatly nicer to look at.
"Elaine Jackson" <el***************@home.com> wrote in message
news:zcxWb.464065$JQ1.270296@pd7tw1no...
b=[x for x in b if x not in c]

"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c0*********@solaris.cc.vt.edu...
| By using lists, I can create sets of number. Suppose I have three lists.
| One list is the super-set, one is a set that contains all the numbers
| (just like the super-set) and the last is sub-set of the super-set. For
| example:
|
| a = [1,2,3,4,5] # The super-set.
| b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
| c = [2,4] # A sub-set
|
| I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
| this would make the sets look like this:
|
| a = [1,2,3,4,5]
| b = [1,3,5]
| c = [2,4]
|
| How do I test set c to find what it contains and then look at set b to
| see if it contains any of those same numbers, and if so, remove them.
|


A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Argue for your limitations, and sure enough they're yours." --Richard Bach
Jul 18 '05 #9
Bart Nessux wrote:
Peter Otten wrote:
Bart Nessux wrote:

By using lists, I can create sets of number. Suppose I have three lists.
One list is the super-set, one is a set that contains all the numbers
(just like the super-set) and the last is sub-set of the super-set. For
example:

a = [1,2,3,4,5] # The super-set.
b = [1,2,3,4,5] # Looks just like the super-set, but it's not.
c = [2,4] # A sub-set

I'd like to remove 2 & 4 from set b BECAUSE they are present in set c...
this would make the sets look like this:

a = [1,2,3,4,5]
b = [1,3,5]
c = [2,4]

How do I test set c to find what it contains and then look at set b to
see if it contains any of those same numbers, and if so, remove them.

You want set operations, so why would you use lists?


All my data are in lists:


All my beer is in sieves.

inputFile = file('ips.txt', 'r') #Super-set
include = inputFile.readlines()
inputFile.close()

# The file below is compiled manually by hand... add IPs to it
# whenever you want to exclude them from IP_protection.
readFile = file('excluded_ips.txt', 'r') #Sub-set to exclude
exclude = readFile.readlines()
readFile.close()

include = [x for x in include if x not in exclude] #Magic of Elaine

outputFile = file('pruned_ips.txt' , 'w')
for i in include:
print>> outputFile, i,
outputFile.close()


(untested)
from sets import set

inputFile = file('ips.txt', 'r') #Super-set
include = Set(inputFile.readlines())
inputFile.close()

readFile = file('excluded_ips.txt', 'r') #Sub-set to exclude
exclude = Set(readFile.readlines())
readFile.close()

# No Magic of Elaine

outputFile = file('pruned_ips.txt' , 'w')
for i in include - exclude:
print >> outputFile, i,
outputFile.close()

Peter
Jul 18 '05 #10
[Peter Otten]
(untested)
from sets import set inputFile = file('ips.txt', 'r') #Super-set
include = Set(inputFile.readlines())
inputFile.close() readFile = file('excluded_ips.txt', 'r') #Sub-set to exclude
exclude = Set(readFile.readlines())
readFile.close() # No Magic of Elaine outputFile = file('pruned_ips.txt' , 'w')
for i in include - exclude:
print >> outputFile, i,
outputFile.close()


Here is an equivalent, shorter algorithm (tested):

from sets import Set
file('pruned_ips.txt', 'w').writelines(
Set(file('ips.txt')) - Set(file('excluded_ips.txt')))

This code relies on `writelines' accepting an iterable, sets returning
their members whenever iterated, Set constructors accepting an iterable,
and files returning their lines whenever iterated. And of course, on
`close' rarely being needed in Python! :-)

The order of lines in the produced file is kind of random, however.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #11
Peter Otten wrote:
outputFile = file('pruned_ips.txt' , 'w')
for i in include - exclude:
print >> outputFile, i,
outputFile.close()


Wow! That makes a lot more sense than the list comprehension stuff. I think
I'll use it. Thanks!
Jul 18 '05 #12
François Pinard wrote:
[Peter Otten]

(untested)
from sets import set


inputFile = file('ips.txt', 'r') #Super-set
include = Set(inputFile.readlines())
inputFile.close()


readFile = file('excluded_ips.txt', 'r') #Sub-set to exclude
exclude = Set(readFile.readlines())
readFile.close()


# No Magic of Elaine


outputFile = file('pruned_ips.txt' , 'w')
for i in include - exclude:
print >> outputFile, i,
outputFile.close()

Here is an equivalent, shorter algorithm (tested):

from sets import Set
file('pruned_ips.txt', 'w').writelines(
Set(file('ips.txt')) - Set(file('excluded_ips.txt')))

This code relies on `writelines' accepting an iterable, sets returning
their members whenever iterated, Set constructors accepting an iterable,
and files returning their lines whenever iterated. And of course, on
`close' rarely being needed in Python! :-)

The order of lines in the produced file is kind of random, however.


Wow! Sets are awesome. I was thinking in terms of lists. A list is like
a set and a set is like a list, but depending on the task at hand, they
have very different applications. Sets work great when one has a
super-set and two sub-sets that need to be compared and modified based
on what they contain and what the super-set contains.

Sets are straight-forward and easy to use too... I can always tell when
I'm trying to do something with a tool that wasn't designed to do what
I'm attempting to do (in this case lists). The task becomes complex and
tedious. Forget about trying to read the code a couple of weeks from now.

Thanks to all for the info on sets!

Jul 18 '05 #13
On Wed, 11 Feb 2004 18:57:15 -0500 in comp.lang.python, François
Pinard <pi****@iro.umontreal.ca> wrote:
[Peter Otten]
(untested)
from sets import set

inputFile = file('ips.txt', 'r') #Super-set
include = Set(inputFile.readlines())
inputFile.close()

readFile = file('excluded_ips.txt', 'r') #Sub-set to exclude
exclude = Set(readFile.readlines())
readFile.close()

# No Magic of Elaine

outputFile = file('pruned_ips.txt' , 'w')
for i in include - exclude:
print >> outputFile, i,
outputFile.close()


Here is an equivalent, shorter algorithm (tested):

from sets import Set
file('pruned_ips.txt', 'w').writelines(
Set(file('ips.txt')) - Set(file('excluded_ips.txt')))

This code relies on `writelines' accepting an iterable, sets returning
their members whenever iterated, Set constructors accepting an iterable,
and files returning their lines whenever iterated. And of course, on
`close' rarely being needed in Python! :-)

The order of lines in the produced file is kind of random, however.


That's very compact and neat, but for completeness I'd like to point
out that it could also be written (more clumsily) in one line with
list comprehensions, retaining the same order of elements as in the
original list:

file('pruned_ips.txt', 'w').writelines([ip for ip in file('ips.txt')
if ip not in file('excluded_ips.txt')])

Of course, your example using sets is much clearer, so I prefer that.

Dave
Jul 18 '05 #14
aa**@pythoncraft.com (Aahz) wrote:

[content at *bottom*]
...
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?


You know, it may be time for this crusade to end. What you say is entirely
valid if a single Usenet post is read out of context. However, when using
a threaded newsreader, as 90% of us do, top-posting allows me to read the
new content without moving my eyes. I just press N, N, N to move to the
next message and scan the new content at the top of the message. In this
particular thread, the content quickly grew larger than my newsreader's
preview pane, so bottom-posting requires me to move the focus to the
preview pane and scroll down to read.

Besides, the MOST annoying thing on Usenet is HTML posts.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #15
> >from sets import Set
file('pruned_ips.txt', 'w').writelines(
Set(file('ips.txt')) - Set(file('excluded_ips.txt')))

[Dave K] file('pruned_ips.txt', 'w').writelines([ip for ip in file('ips.txt')
if ip not in file('excluded_ips.txt')])


The Set solution above swallows both files in memory, but executes
rather quickly. The list comprehension solution uses much less memory,
but as the second file is wholly read for each line of the first file,
it may get prohibitive when files are not small. For very big files,
both solutions are wrong anyway: one should likely disk-sort both files
and do a simultaneous read of the sorted results.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #16
[Tim Roberts]
aa**@pythoncraft.com (Aahz) wrote:
[content at *bottom*]
...
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

You know, it may be time for this crusade to end.
I'm glad that Aahz has the courage of crusading, so sparing some of
mine! :-) In my case at least, crusades are not fun, despite necessary
at times. This particular crusade should only end once people are
educated enough to spontaneously do the proper thing.
In this particular thread, the content quickly grew larger than my
newsreader's preview pane, so bottom-posting requires me to move the
focus to the preview pane and scroll down to read.


People are likely over-quoting, then. Proper quoting is an art, and
when done correctly, quoted material is not an annoyance. On the Python
list, most messages do well in that respect and hopefully, most people
get good habits by mere observation and imitation. It is not bad to
remind people, once in a while, for those who are more slow to get it.

P.S. - Mail or news readers may have an option to hide quoted material.
I sometimes use it in Mutt for messages which are not crafted correctly.

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #17

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

Similar topics

3
by: Simon | last post by:
Hi, I'm hoping you could show me examples of how a functional/declarative language could be used to consicely describe resticted subsets of elements. I'm looking for a 'specification' style...
7
by: Steve | last post by:
This post has two parts. First is my feedback on sets. (Hello? Last summer called, they want their discussion thread back...) Second is some questions about my implementation of a partition...
4
by: Brett Calcott | last post by:
I've found some python solutions to find the set of subsets for a given set, but how do you find the set of the set of subsets whose union is the given set and whose intersections is the empty set....
15
by: les_ander | last post by:
Hi, I have many set objects some of which can contain same group of object while others can be subset of the other. Given a list of sets, I need to get a list of unique sets such that non of the...
7
by: Gaijinco | last post by:
I been thinking about this topic for a long time. The best I have done is the following code: #include <iostream> using namespace std; #include <cmath> int main(){ const int SIZE=3;
25
by: Jessica Weiner | last post by:
I have an array of n integers and I want a function that returns a list of arrays of all possible subsets. Can someone provide me with the code? Thanks. Jess
4
by: LurfysMa | last post by:
I could use some help with a table design problem. I have an electronic flashcard program. Actually, several of them. They each rely on a utility program to keep track of the usage statistics....
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
4
by: Patrick | last post by:
Hi, I want to write a programs that checks if a set of numbers in a list obey a condition, the problem is that i have say "n" numbers and i need to check all subsets of the n numbers for the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.