Package tulip :: Package transys :: Module mathset :: Class MathSet
[frames] | no frames]

Class MathSet


Mathematical set, allows unhashable elements.

Examples

>>> s = MathSet(['a', 1, [1,2], {'a', 'b'} ] )

Then print(s) shows how the elements were separately stored in a set and list, to optimize contains operations:

>>> print(s)
MathSet(['a', 1, [1, 2], set(['a', 'b'])])

Set operations similar to the builtin type are supported:

>>> p = MathSet()
>>> p.add(1)
>>> p |= [1, 2]
>>> p |= {3, 4}
>>> p |= [[1, 2], '5', {'a':1} ]
>>> p.add_from([5, 6, '7', {8, 9} ] )
>>> p.remove(1)
>>> p
MathSet([2, 3, 4, 5, 6, '5', '7', [1, 2], {'a': 1}, set([8, 9])])

See Also

SubSet, PowerSet, set

Instance Methods
 
__init__(self, iterable=[])
Initialize by adding elements from iterable.
 
__repr__(self)
repr(x)
MathSet
__or__(self, other)
Union with another mathematical set.
MathSet (explicit construction)
__mul__(self, other)
Return the Cartesian product with another MathSet.
MathSet
__ior__(self, iterable)
Union with of MathSet with iterable.
 
__sub__(self, rm_items)
 
__isub__(self, rm_items)
Delete multiple elements.
 
__eq__(self, other)
 
__contains__(self, item)
 
__iter__(self)
 
__len__(self)
Number of elements in set.
 
add(self, item)
Add element to mathematical set.
 
add_from(self, iterable)
Add multiple elements to mathematical set.
 
remove(self, item)
Remove existing element from mathematical set.
 
pop(self)
Remove and return random MathSet element.
MathSet
intersection(self, iterable)
Return intersection with iterable.
bool
intersects(self, iterable)
Check intersection with iterable.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__init__(self, iterable=[])
(Constructor)

 

Initialize by adding elements from iterable.

Example

>>> s = MathSet([1, 2, 'a', {3, 4} ] )
Parameters:
  • iterable (iterable, any element types allowed) - iterable from which to initialize the set S which underlies the PowerSet 2^S
Overrides: object.__init__

__repr__(self)
(Representation operator)

 

repr(x)

Overrides: object.__repr__
(inherited documentation)

__or__(self, other)
(Or operator)

 

Union with another mathematical set.

See Also

__ior__

Parameters:
  • other (iterable, elements not restricted to hashable) - any other mathematical set.
Returns: MathSet
self | iterable

__mul__(self, other)

 

Return the Cartesian product with another MathSet.

Example

>>> a = MathSet([1, 2] )
>>> b = MathSet([3, 4] )
>>> c = a *b
>>> print(type(c) )
>>> print(c)

If we prefer a CartesianProduct returned instead: >>> c = a.cartesian(b)

See Also

cartesian

Parameters:
  • other (MathSet) - set with which to take Cartesian product
Returns: MathSet (explicit construction)
Cartesian product of self with other.

__ior__(self, iterable)

 

Union with of MathSet with iterable.

Example

>>> s = MathSet([1, 2] )
>>> s |= [3, 4] # much cleaner & familiar
>>> print(s)
set([1, 2, 3, 4]) U []

See Also

__or__

Parameters:
  • iterable (iterable, elements not restricted to hashable) - any mathematical set.
Returns: MathSet
self | iterable

add(self, item)

 

Add element to mathematical set.

Example

>>> s = MathSet()
>>> s.add(1)
set([1]) U []

See Also

add_from, __ior__, remove

Parameters:
  • item (anything, if hashable it is stored in a Python set, otherwise stored in a list.) - the new set element

add_from(self, iterable)

 

Add multiple elements to mathematical set.

Equivalent to |=

Example

>>> s = MathSet()
>>> s.add_from([1, 2, {3} ] )

is equivalent to:

>>> s = MathSet()
>>> s |= [1, 2, {3} ]

See Also

add, __ior__, remove

Parameters:
  • iterable (iterable containing (possibly not hashable) elements) - new MathSet elements

remove(self, item)

 

Remove existing element from mathematical set.

Example

>>> p = MathSet([1, 2] )
>>> p.remove(1)
>>> p
set([2]) U []

See Also

add, add_from, __or__

Parameters:
  • item - An item already in the set. For adding items, see add.

pop(self)

 

Remove and return random MathSet element.

Raises KeyError if MathSet is empty.

intersection(self, iterable)

 

Return intersection with iterable.

Parameters:
  • iterable (Iterable) - find common elements with self
Returns: MathSet
intersection of self with iterable

intersects(self, iterable)

 

Check intersection with iterable.

Checks the existence of common elements with iterable.

>>> s = MathSet()
>>> s.add(1)
>>> r = [1,2]
>>> s.intersects(r)
True
Parameters:
  • iterable (Iterable) - with which to check intersection
Returns: bool
True if self has common element with iterable. Otherwise False.