CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-multiset

An implementation of a multiset data structure with both mutable and immutable variants supporting all standard set operations.

Pending
Overview
Eval results
Files

element-management.mddocs/

Element Management

Core functionality for accessing, checking, and inspecting individual elements in multisets. Provides dictionary-like interface for element access and comprehensive methods for examining multiset contents.

Capabilities

Element Access and Membership

Access element multiplicities and test membership with dictionary-like interface.

def __contains__(self, element) -> bool:
    """
    Test if element is present in the multiset.
    
    Parameters:
    - element: Element to test for membership
    
    Returns:
    True if element has multiplicity > 0
    """

def __getitem__(self, element) -> int:
    """
    Get the multiplicity of an element.
    
    Parameters:
    - element: Element to get multiplicity for
    
    Returns:
    Multiplicity of element (0 if not present)
    """

def get(self, element, default: int) -> int:
    """
    Get the multiplicity of an element with optional default.
    
    Parameters:
    - element: Element to get multiplicity for
    - default: Value to return if element not present
    
    Returns:
    Multiplicity of element or default value
    """

Usage Examples:

from multiset import Multiset

ms = Multiset('aaabbc')   # {'a': 3, 'b': 2, 'c': 1}

# Membership testing
'a' in ms                 # True
'z' in ms                 # False

# Get multiplicities
ms['a']                   # 3
ms['z']                   # 0 (not present)
ms.get('a')               # 3
ms.get('z', -1)           # -1 (using provided default)

Collection Views and Iteration

Access different views of multiset contents for inspection and iteration.

def distinct_elements(self) -> KeysView:
    """
    Return a view of unique elements in the multiset.
    
    Returns:
    KeysView containing each unique element once
    """

def multiplicities(self) -> ValuesView:
    """
    Return a view of all multiplicity values.
    
    Returns:
    ValuesView containing multiplicity of each unique element
    """

def values(self) -> ValuesView:
    """
    Return a view of all multiplicity values (alias for multiplicities).
    
    Returns:
    ValuesView containing multiplicity of each unique element
    """

def items(self) -> ItemsView:
    """
    Return a view of (element, multiplicity) pairs.
    
    Returns:
    ItemsView containing (element, multiplicity) tuples
    """

def __iter__(self) -> Iterator:
    """
    Iterate over all elements with repetition based on multiplicity.
    
    Returns:
    Iterator yielding each element according to its multiplicity
    """

def __len__(self) -> int:
    """
    Return total number of elements (sum of all multiplicities).
    
    Returns:
    Total count of elements including repetitions
    """

def __bool__(self) -> bool:
    """
    Return True if multiset is non-empty.
    
    Returns:
    True if multiset contains any elements
    """

def from_elements(cls, elements, multiplicity: int):
    """
    Create multiset where all elements have the same multiplicity.
    
    Parameters:
    - elements: Iterable of elements to include
    - multiplicity: Multiplicity to assign to each element
    
    Returns:
    New multiset with uniform multiplicities
    """

def __copy__(self):
    """Support for copy.copy() - alias for copy() method."""

Usage Examples:

ms = Multiset('aaabbc')   # {'a': 3, 'b': 2, 'c': 1}

# View unique elements
list(ms.distinct_elements())   # ['a', 'b', 'c']
# View multiplicities
list(ms.multiplicities())     # [3, 2, 1]
# View multiplicities (alias)
list(ms.values())             # [3, 2, 1]

# View element-multiplicity pairs
list(ms.items())              # [('a', 3), ('b', 2), ('c', 1)]

# Iterate with repetition
list(ms)                      # ['a', 'a', 'a', 'b', 'b', 'c']

# Size and boolean tests
len(ms)                       # 6 (total elements)
bool(ms)                      # True (non-empty)
bool(Multiset())              # False (empty)

String Representations

Get readable string representations of multiset contents.

def __str__(self) -> str:
    """
    Return string representation showing all elements with repetition.
    
    Returns:
    String containing all elements (like list representation)
    """

def __repr__(self) -> str:
    """
    Return detailed string representation showing multiplicities.
    
    Returns:
    String showing constructor call with element-multiplicity mapping
    """

Usage Examples:

ms = Multiset('aab')

str(ms)                   # "['a', 'a', 'b']"
repr(ms)                  # "Multiset({'a': 2, 'b': 1})"
print(ms)                 # ['a', 'a', 'b']

Construction and Copying

Create new multisets and copies with various initialization patterns.

def __init__(self, iterable = None):
    """
    Initialize multiset from iterable or mapping.
    
    Parameters:
    - iterable: Optional iterable of elements or mapping of element->multiplicity
    """

def copy(self) -> BaseMultiset:
    """
    Create a shallow copy of the multiset.
    
    Returns:
    New multiset with same elements and multiplicities
    """

def __copy__(self) -> BaseMultiset:
    """Support for copy.copy()."""

@classmethod
def from_elements(cls, elements, multiplicity: int):
    """
    Create multiset where all elements have the same multiplicity.
    
    Parameters:
    - elements: Iterable of elements to include
    - multiplicity: Multiplicity to assign to each element
    
    Returns:
    New multiset with uniform multiplicities
    """

Usage Examples:

import copy

# Various construction methods
ms1 = Multiset('aab')                    # From string
ms2 = Multiset(['a', 'a', 'b'])          # From list
ms3 = Multiset({'a': 2, 'b': 1})         # From mapping
ms4 = Multiset.from_elements('abc', 3)   # All elements with count 3

# Copying
ms_copy = ms1.copy()                     # Explicit copy
ms_copy2 = copy.copy(ms1)                # Using copy module (__copy__ method)

# Class method construction
ms4 = Multiset.from_elements('abc', 3)   # All elements with count 3: {'a': 3, 'b': 3, 'c': 3}

# Verify independence
ms1.add('c')
ms_copy == ms1                           # False (independent copies)

Element Counting and Statistics

Additional utilities for analyzing multiset contents.

def __len__(self) -> int:
    """Total number of elements (sum of multiplicities)."""

def __bool__(self) -> bool:
    """True if multiset contains any elements."""

Usage Examples:

ms = Multiset('aaabbc')

# Basic statistics
len(ms)                       # 6 (total elements)
len(ms.distinct_elements())   # 3 (unique elements)
max(ms.multiplicities())      # 3 (highest multiplicity)
min(ms.multiplicities())      # 1 (lowest multiplicity)
sum(ms.multiplicities())      # 6 (same as len(ms))

# Check if empty
bool(Multiset())              # False
bool(ms)                      # True

Serialization Support

Support for pickling multisets for persistence.

def __getstate__(self):
    """Support for pickle serialization."""

def __setstate__(self, state):
    """Support for pickle deserialization."""

Usage Examples:

import pickle

ms = Multiset('aab')

# Serialize and deserialize
data = pickle.dumps(ms)
ms_restored = pickle.loads(data)

ms == ms_restored            # True (preserved contents)

Install with Tessl CLI

npx tessl i tessl/pypi-multiset

docs

element-management.md

index.md

mutable-operations.md

set-operations.md

tile.json