Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.
—
Functions for working with indices and positions in sequences, particularly useful for combinatorial operations and mathematical indexing.
Functions for converting between combinations and their lexicographic indices.
def combination_index(element: Iterable[Any], pool: Iterable[Any]) -> int: ...
def combination_with_replacement_index(element: Iterable[Any], pool: Iterable[Any]) -> int: ...
def nth_combination(iterable: Iterable[Any], r: int, index: int) -> tuple[Any, ...]: ...
def nth_combination_with_replacement(iterable: Iterable[Any], r: int, index: int) -> tuple[Any, ...]: ...Usage:
from more_itertools import combination_index, nth_combination
# Get index of a specific combination
pool = 'ABCD'
combo = ('A', 'C')
index = combination_index(combo, pool) # 1
# Get combination at specific index
nth_combo = nth_combination(pool, 2, 1) # ('A', 'C')
# Verify round-trip
combo == nth_combo # TrueFunctions for converting between permutations and their lexicographic indices.
def permutation_index(element: Iterable[Any], pool: Iterable[Any]) -> int: ...
def nth_permutation(iterable: Iterable[Any], r: int, index: int) -> tuple[Any, ...]: ...Usage:
from more_itertools import permutation_index, nth_permutation
# Get index of a specific permutation
pool = 'ABC'
perm = ('B', 'A', 'C')
index = permutation_index(perm, pool) # 3
# Get permutation at specific index
nth_perm = nth_permutation(pool, 3, 3) # ('B', 'A', 'C')Functions for working with Cartesian product indices.
def product_index(element: Iterable[Any], pools: Iterable[Iterable[Any]], *, repeat: int = 1) -> int: ...
def nth_product(*pools: Iterable[Any], repeat: int = 1, index: int) -> tuple[Any, ...]: ...Usage:
from more_itertools import product_index, nth_product
# Get index of a specific product element
pools = ['AB', '12']
element = ('B', '1')
index = product_index(element, pools) # 2
# Get product element at specific index
nth_elem = nth_product('AB', '12', index=2) # ('B', '1')Functions for iterating over indices in various patterns.
def iter_index(iterable: Iterable[Any], value: Any, *, start: int = 0, stop: int = None) -> Iterator[int]: ...Usage:
from more_itertools import iter_index
# Find all indices of a specific value
data = ['a', 'b', 'a', 'c', 'a', 'd']
indices = list(iter_index(data, 'a')) # [0, 2, 4]
# Find indices within a range
indices = list(iter_index(data, 'a', start=1, stop=4)) # [2]Install with Tessl CLI
npx tessl i tessl/pypi-more-itertools