Sets in Python

Laurent Szyster l.szyster at ibm.net
Thu Feb 1 10:29:47 EST 2001


Magnus Lie Hetland wrote:
> 
> I quite often use dictionaries as sets in Python... But
> it doesn't really seem pretty enough to me... Some
> arbitrariness (as with "while 1: ... break" I still
> don't like that one...) about using what to me is a
> value as a key, and assigning it some value -- like 1,
> for instance... And then checking it with has_key, and
> removing it with del...

if you are looking for set manipulations, get the
kjbuckets module and do:

>>> from kjbuckets import kjSet
>>> A = kjSet ([1,2,3,4,'X','Y'])
>>> A.has_key ('X')
1
>>> B = kjSet ([1,2,5,'Z'])
>>> A-B
kjSet([3, 4, 'X', 'Y'])
>>> A+B
kjSet([1, 2, 3, 4, 'X', 'Y', 5, 'Z'])
>>> A&B
kjSet([1, 2])

The module also provides a graph class (and everything
you need to write your own relational algebra, since
it's been developped by the Gadfly author).


Laurent Szyster



More information about the Python-list mailing list