# sets and subsets

Peter Otten __peter__ at web.de
Wed Feb 11 23:22:21 CET 2004

```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
> 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
>
> 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
inputFile.close()

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

# No Magic of Elaine

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

Peter

```