An implementation of a multiset data structure with both mutable and immutable variants supporting all standard set operations.
npx @tessl/cli install tessl/pypi-multiset@3.2.0An implementation of a multiset data structure for Python that allows elements to occur multiple times. Provides both mutable (Multiset) and immutable (FrozenMultiset) variants with full support for set operations including union, intersection, difference, and symmetric difference.
pip install multisetfrom multiset import Multiset, FrozenMultiset, BaseMultisetfrom multiset import Multiset, FrozenMultiset
# Create a mutable multiset
ms1 = Multiset('aab')
ms2 = Multiset(['a', 'b', 'c'])
# Set operations
union_result = ms1 | ms2 # Union: ['a', 'a', 'b', 'c']
intersection = ms1 & ms2 # Intersection: ['a', 'b']
difference = ms1 - ms2 # Difference: ['a']
# Check membership and get multiplicity
'a' in ms1 # True
ms1['a'] # 2 (multiplicity of 'a')
# Modify multiset
ms1.add('c', multiplicity=2) # Add 'c' twice
ms1.remove('a') # Remove one 'a'
# Create immutable multiset
frozen_ms = FrozenMultiset('abc')
hash(frozen_ms) # Hashable, can be used in sets/dictsThe multiset implementation is based on three main classes:
All variants use efficient dictionary mapping internally where keys are elements and values are their multiplicities. Zero-count elements are automatically removed.
Mathematical set operations supporting both multiset and regular set operands. Operations include union (maximum multiplicities), intersection (minimum multiplicities), difference, symmetric difference, and combination (sum multiplicities).
def union(self, *others) -> BaseMultiset: ...
def intersection(self, *others) -> BaseMultiset: ...
def difference(self, *others) -> BaseMultiset: ...
def symmetric_difference(self, other) -> BaseMultiset: ...
def combine(self, *others) -> BaseMultiset: ...
def times(self, factor: int) -> BaseMultiset: ...
def isdisjoint(self, other) -> bool: ...
def issubset(self, other) -> bool: ...
def issuperset(self, other) -> bool: ...Core functionality for accessing, checking, and manipulating individual elements including membership testing, multiplicity access, and element counting with dictionary-like interface.
def __contains__(self, element) -> bool: ...
def __getitem__(self, element) -> int: ...
def get(self, element, default: int) -> int: ...
def distinct_elements(self) -> KeysView: ...
def multiplicities(self) -> ValuesView: ...
def values(self) -> ValuesView: ...
def copy(self) -> BaseMultiset: ...
@classmethod
def from_elements(cls, elements, multiplicity: int) -> BaseMultiset: ...Mutating operations available only on Multiset instances for modifying multiset contents including adding/removing elements, updating from other collections, and in-place set operations.
def add(self, element, multiplicity: int = 1) -> None: ...
def remove(self, element, multiplicity: Optional[int] = None) -> int: ...
def discard(self, element, multiplicity: Optional[int] = None) -> int: ...
def update(self, *others, **kwargs) -> None: ...
def clear(self) -> None: ...
def pop(self, element, default: int) -> int: ...
def setdefault(self, element, default: int) -> int: ...class BaseMultiset(MappingType[_TElement, int], Generic[_TElement]):
"""Abstract base multiset implementation."""
def __init__(self, iterable: Optional[Union[Iterable[_TElement], Mapping[_TElement, int]]] = None): ...
def __len__(self) -> int: ...
def __bool__(self) -> bool: ...
def __iter__(self) -> Iterator[_TElement]: ...
def copy(self) -> BaseMultiset: ...
class Multiset(BaseMultiset[_TElement], MutableMappingType[_TElement, int]):
"""Mutable multiset implementation."""
def __setitem__(self, element: _TElement, multiplicity: int) -> None: ...
def __delitem__(self, element: _TElement) -> None: ...
class FrozenMultiset(BaseMultiset[_TElement]):
"""Immutable, hashable multiset implementation."""
def __hash__(self) -> int: ...
# Type aliases
_TElement = TypeVar('_TElement', bound=Hashable)
_OtherType = Union[Iterable[_TElement], Mapping[_TElement, int]]