CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-more-itertools

Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.

Pending
Overview
Eval results
Files

windowing.mddocs/

Windowing Operations

Functions for creating sliding windows and overlapping views of data.

Capabilities

Basic Windowing

Create sliding windows over iterables.

def windowed(iterable, n, fillvalue=None, step=1):
    """
    Create sliding window of size n over iterable.
    
    Args:
        iterable: Input iterable to window
        n: Window size
        fillvalue: Value to use for padding incomplete windows
        step: Step size between windows
        
    Returns:
        Iterator of n-tuples representing windows
    """

def windowed_complete(iterable, n):
    """
    Create sliding windows, yielding only complete windows.
    
    Args:
        iterable: Input iterable to window
        n: Window size
        
    Returns:
        Iterator of n-tuples (no incomplete windows)
    """

def sliding_window(iterable, n):
    """
    Create sliding window of size n.
    
    Args:
        iterable: Input iterable to window
        n: Window size
        
    Returns:
        Iterator of n-tuples representing sliding windows
    """

Usage Examples:

from more_itertools import windowed, sliding_window

# Basic sliding window
data = [1, 2, 3, 4, 5]
windows = list(windowed(data, 3))
# Result: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

# With fillvalue for incomplete windows
windows = list(windowed(data, 3, fillvalue='X'))
# Result: [(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 'X'), (5, 'X', 'X')]

# Step size greater than 1
windows = list(windowed(data, 3, step=2))
# Result: [(1, 2, 3), (3, 4, 5)]

Pairwise and N-wise Operations

Generate adjacent pairs, triples, and n-tuples.

def pairwise(iterable):
    """
    Return successive overlapping pairs from iterable.
    
    Args:
        iterable: Input iterable
        
    Returns:
        Iterator of 2-tuples (adjacent pairs)
    """

def triplewise(iterable):
    """
    Return successive overlapping triples from iterable.
    
    Args:
        iterable: Input iterable
        
    Returns:
        Iterator of 3-tuples (adjacent triples)
    """

Usage Examples:

from more_itertools import pairwise, triplewise

# Adjacent pairs
pairs = list(pairwise([1, 2, 3, 4, 5]))
# Result: [(1, 2), (2, 3), (3, 4), (4, 5)]

# Adjacent triples  
triples = list(triplewise([1, 2, 3, 4, 5]))
# Result: [(1, 2, 3), (2, 3, 4), (3, 4, 5)]

Staggered Views

Create lagged or offset views of data.

def stagger(iterable, offsets=(0, 1), longest=False, fillvalue=None):
    """
    Create multiple offset iterators from single iterable.
    
    Args:
        iterable: Input iterable to stagger
        offsets: Sequence of offset values
        longest: If True, iterate until longest iterator exhausted
        fillvalue: Value for padding when longest=True
        
    Returns:
        Iterator of tuples with staggered values
    """

Usage Examples:

from more_itertools import stagger

# Basic staggering
data = [0, 1, 2, 3, 4, 5]
staggered = list(stagger(data, offsets=(0, 2)))
# Result: [(0, 2), (1, 3), (2, 4), (3, 5)]

# Multiple offsets
staggered = list(stagger(data, offsets=(0, 1, 2)))
# Result: [(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]

# With longest=True
staggered = list(stagger(data, offsets=(0, 2), longest=True, fillvalue=None))
# Result: [(0, 2), (1, 3), (2, 4), (3, 5), (4, None), (5, None)]

Substring Operations

Generate substrings and substring-like operations.

def substrings(iterable):
    """
    Generate all non-empty contiguous subsequences.
    
    Args:
        iterable: Input iterable
        
    Returns:
        Iterator of all contiguous subsequences
    """

def substrings_indexes(seq, reverse=False):
    """
    Generate all substrings with their start and end indexes.
    
    Args:
        seq: Input sequence
        reverse: If True, generate in reverse order
        
    Returns:
        Iterator of (substring, start_index, end_index) tuples
    """

def subslices(iterable):
    """
    Generate all contiguous subslices as lists.
    
    Args:
        iterable: Input iterable
        
    Returns:
        Iterator of lists (all contiguous subslices)
    """

Usage Examples:

from more_itertools import substrings, substrings_indexes

# All substrings
subs = list(substrings("abc"))
# Result: [('a',), ('a', 'b'), ('a', 'b', 'c'), ('b',), ('b', 'c'), ('c',)]

# With indexes
subs_idx = list(substrings_indexes("abc"))
# Result: [('a', 0, 1), ('ab', 0, 2), ('abc', 0, 3), ('b', 1, 2), ('bc', 1, 3), ('c', 2, 3)]

Install with Tessl CLI

npx tessl i tessl/pypi-more-itertools

docs

advanced-utilities.md

combinatorics.md

combining.md

comparison.md

grouping.md

index.md

indexing.md

iteration-utilities.md

lookahead.md

mathematical.md

random-operations.md

selecting.md

sequence-utilities.md

special-purpose.md

summarizing.md

uniqueness.md

utility-classes.md

windowing.md

tile.json