IndexSetND#
- class IndexSetND(*iterables, names=None)[source]#
Custom list-like data structure to define index-sets with N-dim tuple elements.
Requires all elements to be unique tuples of the same length, each containing ‘N’ scalars (such as int, str, Timestamp, etc.). Supports all mutable sequence operations from builtins.list and rich comparisons from builtins.set. Allows efficient selection of subsets using wildcard patterns with the subset method and position indices with the squeeze method.
- Parameters:
- *iterablesiterable, optional
Input data to be encapsulated in the IndexSet, in one of the following forms:
An iterable of unique tuples of the same length, each containing ‘N’ scalars.
An iterable of unique tuple-like containers of the same length, each containing ‘N’ scalars - will be coerced to tuples.
An arbitrary number of iterables of scalars or K-dim tuple-like containers or a mix of both - will enumerate all possible combinations and construct tuples composed of ‘N’ scalars.
See examples below.
- namessequence[str], optional
Names to refer to each dimension of N-dim tuple elements - not used internally, and solely for user reference.
- Raises:
- TypeError
If input includes (or creates) non-tuple element(s).
- ValueError
If input includes (or creates) tuple elements of different lengths.
- ValueError
If input includes (or creates) duplicate elements.
See also
IndexSet1DFor 1-dim scalar elements.
Examples
Constructing empty to populate later:
>>> IndexSetND() IndexSetND: []
Constructing with a list of tuples:
>>> IndexSetND( ... [('chair', 0), ('chair', 1), ('chair', 2), ('desk', 0), ('desk', 1), ('desk', 2)], ... names=['PRODUCT', 'PERIOD'], ... ) IndexSetND: (PRODUCT, PERIOD) [('chair', 0), ('chair', 1), ('chair', 2), ('desk', 0), ('desk', 1), ('desk', 2)]
Constructing with an arbitrary number of iterables to enumerate all possible combinations:
>>> IndexSetND(range(2), range(2), range(2)) IndexSetND: [(0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 1, 0), (1, 1, 1)]
>>> IndexSetND(['chair', 'desk'], range(3), names=['PRODUCT', 'PERIOD']) IndexSetND: (PRODUCT, PERIOD) [('chair', 0), ('chair', 1), ('chair', 2), ('desk', 0), ('desk', 1), ('desk', 2)]
>>> IndexSetND( ... [('chair', 'WH-A'), ('chair', 'WH-B'), ('desk', 'WH-B')], ... range(2), ... names=['PRODUCT', 'WAREHOUSE', 'PERIOD'], ... ) IndexSetND: (PRODUCT, WAREHOUSE, PERIOD) [('chair', 'WH-A', 0), ('chair', 'WH-A', 1), ('chair', 'WH-B', 0), ('chair', 'WH-B', 1), ('desk', 'WH-B', 0), ('desk', 'WH-B', 1)]
Methods
IndexSetND.append(elem, /)Append an element to the end of the IndexSet, in-place.
Remove all elements from the IndexSet.
IndexSetND.difference(*others)Return a new IndexSetND with elements in the IndexSetND that are not in the others.
IndexSetND.extend(elems, /)Extend the IndexSet by appending elements from an iterable, in-place.
IndexSetND.index(elem[, start, end])Get the position index of an element in the IndexSet.
IndexSetND.insert(index, elem, /)Insert an element at a position index in the IndexSet.
IndexSetND.intersection(*others)Return a new IndexSetND with elements common to the IndexSetND and all others.
IndexSetND.isdisjoint(other)Whether the IndexSet has no elements in common with another iterable, or not.
IndexSetND.pop([index])Remove and return the element at a position index in the IndexSet.
IndexSetND.remove(elem, /)Remove an element from the IndexSet, in-place.
Reverse the order of elements of the IndexSet, in-place.
IndexSetND.sort(*[, key, reverse])Sort the IndexSet in ascending order, in-place.
IndexSetND.squeeze(*indices[, names])Squeeze the IndexSet for given dimension indices to get a new IndexSet.
IndexSetND.subset(*pattern)Get a subset of the IndexSet with a wildcard pattern.
Return a new IndexSetND with elements in either the IndexSetND or other but not both.
IndexSetND.union(*others)Return a new IndexSetND with elements from the IndexSetND and all others.
Attributes
Names to refer to each dimension of N-dim tuple elements.