Additional building blocks, recipes, and routines for working with Python iterables beyond itertools.
—
Functions for generating permutations, combinations, and related structures.
Generate combinations and permutations with various constraints.
def powerset(iterable):
"""
Generate powerset of iterable (all possible subsets).
Args:
iterable: Input iterable
Returns:
Iterator of tuples representing all subsets
"""
def distinct_combinations(iterable, r):
"""
Generate combinations of distinct items only.
Args:
iterable: Input iterable
r: Length of combinations
Returns:
Iterator of r-tuples with distinct items only
"""
def distinct_permutations(iterable, r=None):
"""
Generate permutations of distinct items only.
Args:
iterable: Input iterable
r: Length of permutations (None for full length)
Returns:
Iterator of r-tuples representing distinct permutations
"""Usage Examples:
from more_itertools import powerset, distinct_combinations, distinct_permutations
# Powerset
ps = list(powerset([1, 2, 3]))
# Result: [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
# Distinct combinations
dc = list(distinct_combinations([1, 1, 2], 2))
# Result: [(1, 2)] (removes duplicate (1, 1))
# Distinct permutations
dp = list(distinct_permutations([1, 1, 2]))
# Result: [(1, 1, 2), (1, 2, 1), (2, 1, 1)]Specialized combinatorics for sets and partitions.
def powerset_of_sets(iterable):
"""
Generate powerset where each subset is a frozenset.
Args:
iterable: Input iterable
Returns:
Iterator of frozensets representing all subsets
"""
def set_partitions(iterable, k=None):
"""
Generate all set partitions of iterable.
Args:
iterable: Input iterable to partition
k: Number of parts (None for all possible)
Returns:
Iterator of set partitions (each partition is tuple of frozensets)
"""
def partitions(n, m=None):
"""
Generate integer partitions of n.
Args:
n: Integer to partition
m: Maximum part size (None for unlimited)
Returns:
Iterator of tuples representing integer partitions
"""Functions for cyclic arrangements and circular shifts.
def circular_shifts(iterable):
"""
Generate all circular shifts of iterable.
Args:
iterable: Input iterable to shift
Returns:
Iterator of tuples representing all circular shifts
"""
def derangements(iterable, r=None):
"""
Generate all derangements (permutations with no fixed points).
Args:
iterable: Input iterable
r: Length of derangements (None for full length)
Returns:
Iterator of derangement tuples
"""Usage Examples:
from more_itertools import circular_shifts, partitions
# Circular shifts
shifts = list(circular_shifts([1, 2, 3]))
# Result: [(1, 2, 3), (2, 3, 1), (3, 1, 2)]
# Integer partitions of 4
parts = list(partitions(4))
# Result: [(4,), (3, 1), (2, 2), (2, 1, 1), (1, 1, 1, 1)]Cartesian product variations and outer products.
def gray_product(*iterables):
"""
Cartesian product in Gray code order.
Args:
*iterables: Iterables to take product of
Returns:
Iterator of tuples in Gray code order
"""
def outer_product(func, xs, ys):
"""
Compute outer product using binary function.
Args:
func: Binary function to apply
xs: First iterable
ys: Second iterable
Returns:
Iterator of outer product results
"""
def partial_product(*iterables):
"""
Generate partial products of iterables.
Args:
*iterables: Iterables to take partial products of
Returns:
Iterator of partial product tuples
"""Functions that work with combinatorial indices.
def combination_index(element, iterable):
"""
Get lexicographic index of combination.
Args:
element: Combination tuple
iterable: Source iterable
Returns:
Index of combination in lexicographic order
"""
def combination_with_replacement_index(element, iterable):
"""
Get index of combination with replacement.
Args:
element: Combination tuple (with replacement)
iterable: Source iterable
Returns:
Index in lexicographic order
"""
def permutation_index(element, iterable):
"""
Get lexicographic index of permutation.
Args:
element: Permutation tuple
iterable: Source iterable
Returns:
Index of permutation in lexicographic order
"""
def product_index(element, *iterables, repeat=1):
"""
Get index of element in Cartesian product.
Args:
element: Product tuple
*iterables: Source iterables
repeat: Number of repetitions
Returns:
Index in product ordering
"""Generate specific combinatorial elements by index.
def nth_combination(iterable, r, index):
"""
Get nth combination in lexicographic order.
Args:
iterable: Source iterable
r: Combination length
index: Index of desired combination
Returns:
Tuple representing nth combination
"""
def nth_permutation(iterable, r, index):
"""
Get nth permutation in lexicographic order.
Args:
iterable: Source iterable
r: Permutation length
index: Index of desired permutation
Returns:
Tuple representing nth permutation
"""
def nth_product(index, *args):
"""
Get nth element of Cartesian product by index.
Args:
index: Index of desired element
*args: Source iterables for product
Returns:
Tuple representing nth product element
"""
def nth_combination_with_replacement(iterable, r, index):
"""
Get nth combination with replacement.
Args:
iterable: Source iterable
r: Combination length
index: Index of desired combination
Returns:
Tuple representing nth combination with replacement
"""Generate random combinatorial selections.
def random_product(*iterables, repeat=1):
"""
Generate random element from Cartesian product.
Args:
*iterables: Source iterables
repeat: Number of repetitions
Returns:
Random tuple from product
"""
def random_permutation(iterable, r=None):
"""
Generate random permutation.
Args:
iterable: Source iterable
r: Permutation length (None for full length)
Returns:
Random permutation tuple
"""
def random_combination(iterable, r):
"""
Generate random combination.
Args:
iterable: Source iterable
r: Combination length
Returns:
Random combination tuple
"""
def random_combination_with_replacement(iterable, r):
"""
Generate random combination with replacement.
Args:
iterable: Source iterable
r: Combination length
Returns:
Random combination tuple (with replacement)
"""Usage Examples:
from more_itertools import nth_combination, random_combination
# Get 3rd combination of length 2 from [1,2,3,4]
combo = nth_combination([1, 2, 3, 4], 2, 3)
# Result: (2, 4)
# Random combination
import random
random.seed(42)
rand_combo = random_combination([1, 2, 3, 4, 5], 3)
# Result: Random 3-element combinationInstall with Tessl CLI
npx tessl i tessl/pypi-more-itertools