0
# Indexing and Position Operations
1
2
Functions for working with indices and positions in sequences, particularly useful for combinatorial operations and mathematical indexing.
3
4
## Capabilities
5
6
### Combination Indexing
7
8
Functions for converting between combinations and their lexicographic indices.
9
10
```python { .api }
11
def combination_index(element: Iterable[Any], pool: Iterable[Any]) -> int: ...
12
def combination_with_replacement_index(element: Iterable[Any], pool: Iterable[Any]) -> int: ...
13
def nth_combination(iterable: Iterable[Any], r: int, index: int) -> tuple[Any, ...]: ...
14
def nth_combination_with_replacement(iterable: Iterable[Any], r: int, index: int) -> tuple[Any, ...]: ...
15
```
16
17
**Usage:**
18
19
```python
20
from more_itertools import combination_index, nth_combination
21
22
# Get index of a specific combination
23
pool = 'ABCD'
24
combo = ('A', 'C')
25
index = combination_index(combo, pool) # 1
26
27
# Get combination at specific index
28
nth_combo = nth_combination(pool, 2, 1) # ('A', 'C')
29
30
# Verify round-trip
31
combo == nth_combo # True
32
```
33
34
### Permutation Indexing
35
36
Functions for converting between permutations and their lexicographic indices.
37
38
```python { .api }
39
def permutation_index(element: Iterable[Any], pool: Iterable[Any]) -> int: ...
40
def nth_permutation(iterable: Iterable[Any], r: int, index: int) -> tuple[Any, ...]: ...
41
```
42
43
**Usage:**
44
45
```python
46
from more_itertools import permutation_index, nth_permutation
47
48
# Get index of a specific permutation
49
pool = 'ABC'
50
perm = ('B', 'A', 'C')
51
index = permutation_index(perm, pool) # 3
52
53
# Get permutation at specific index
54
nth_perm = nth_permutation(pool, 3, 3) # ('B', 'A', 'C')
55
```
56
57
### Product Indexing
58
59
Functions for working with Cartesian product indices.
60
61
```python { .api }
62
def product_index(element: Iterable[Any], pools: Iterable[Iterable[Any]], *, repeat: int = 1) -> int: ...
63
def nth_product(*pools: Iterable[Any], repeat: int = 1, index: int) -> tuple[Any, ...]: ...
64
```
65
66
**Usage:**
67
68
```python
69
from more_itertools import product_index, nth_product
70
71
# Get index of a specific product element
72
pools = ['AB', '12']
73
element = ('B', '1')
74
index = product_index(element, pools) # 2
75
76
# Get product element at specific index
77
nth_elem = nth_product('AB', '12', index=2) # ('B', '1')
78
```
79
80
### Index Iteration
81
82
Functions for iterating over indices in various patterns.
83
84
```python { .api }
85
def iter_index(iterable: Iterable[Any], value: Any, *, start: int = 0, stop: int = None) -> Iterator[int]: ...
86
```
87
88
**Usage:**
89
90
```python
91
from more_itertools import iter_index
92
93
# Find all indices of a specific value
94
data = ['a', 'b', 'a', 'c', 'a', 'd']
95
indices = list(iter_index(data, 'a')) # [0, 2, 4]
96
97
# Find indices within a range
98
indices = list(iter_index(data, 'a', start=1, stop=4)) # [2]
99
```