sets and subsets

Bart Nessux bart_nessux at hotmail.com
Thu Feb 12 16:04:20 CET 2004


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!




More information about the Python-list mailing list