operators and datatypes for sets

Anton Vredegoor anton at vredegoor.doge.nl
Mon Apr 23 11:59:17 EDT 2001


There has been some discussion about sets some time ago, but I could
find no conclusive information. The consensus seemed to be to use a
dictionary as the underlying data type. In order to revive the
discussion (or to make it explicit here, if it was not dead but only
invisible to me) I would like to present some test code I have made. I
would post it here but since it is 142 lines long I will just give a
link:

http://home.hccnet.nl/a.vredegoor/smallset/smallset.py

or the html version:

http://home.hccnet.nl/a.vredegoor/smallset/smallset.html

The basic idea is to use lists as the underlying datatype and to do
member administration by setting bits to '1' or '0' in a longint. This
has the advantage of doing very fast operations. There are also some
disadvantages, one of them is having to specify a 'universe'.

There is a also a PEP about this,

http://python.sourceforge.net/peps/pep-0218.html

but this PEP is about a language change, which is way out of my league
since I am only talking about a module.

I have used the 'in' operator for determining if a set is a subset of
another set. This is different from its normal use where it is used
for checking if an element is in a set. Further I have used the '/'
operator for 'set1 without the elements in set2'. This is also
unusual. I have also added a new operator '~' which can be available
because the universe is specified. It gives all elements that are not
in the set. These are just suggestions.

Please let me know what you think or show me some better code,
improvements would also be very welcome.

Anton.




More information about the Python-list mailing list