iterables¶
Operations on iterables.
TypeVar’s
- class T¶
TypeVar('T')
- class U¶
TypeVar('U')
- class F¶
TypeVar('F', np.float64, np.int_)
- class H¶
TypeVar('H', bound=Hashable)
Functions
- adjacent_n_tuples(objects, n)[source]¶
Returns the Sequence objects cyclically split into n length tuples.
See also
adjacent_pairsalias with n=2
Examples
>>> list(adjacent_n_tuples([1, 2, 3, 4], 2)) [(1, 2), (2, 3), (3, 4), (4, 1)] >>> list(adjacent_n_tuples([1, 2, 3, 4], 3)) [(1, 2, 3), (2, 3, 4), (3, 4, 1), (4, 1, 2)]
- adjacent_pairs(objects)[source]¶
Alias for
adjacent_n_tuples(objects, 2).See also
Examples
>>> list(adjacent_pairs([1, 2, 3, 4])) [(1, 2), (2, 3), (3, 4), (4, 1)]
- all_elements_are_instances(iterable, Class)[source]¶
Returns
Trueif all elements of iterable are instances of Class. False otherwise.- Parameters:
iterable (Iterable[object])
Class (type[object])
- Return type:
bool
- batch_by_property(items, property_func)[source]¶
Takes in a Sequence, and returns a list of tuples, (batch, prop) such that all items in a batch have the same output when put into the Callable property_func, and such that chaining all these batches together would give the original Sequence (i.e. order is preserved).
Examples
>>> batch_by_property([(1, 2), (3, 4), (5, 6, 7), (8, 9)], len) [([(1, 2), (3, 4)], 2), ([(5, 6, 7)], 3), ([(8, 9)], 2)]
- concatenate_lists(*list_of_lists)[source]¶
Combines the Iterables provided as arguments into one list.
Examples
>>> concatenate_lists([1, 2], [3, 4], [5]) [1, 2, 3, 4, 5]
- hash_obj(obj)[source]¶
Determines a hash, even of potentially mutable objects.
- Parameters:
obj (object)
- Return type:
int
- list_difference_update(l1, l2)[source]¶
Returns a list containing all the elements of l1 not in l2.
Examples
>>> list_difference_update([1, 2, 3, 4], [2, 4]) [1, 3]
- list_update(l1, l2)[source]¶
- Used instead of
set.update()to maintain order, making sure duplicates are removed from l1, not l2. Removes overlap of l1 and l2 and then concatenates l2 unchanged.
Examples
>>> list_update([1, 2, 3], [2, 4, 4]) [1, 3, 2, 4, 4]
- Used instead of
- listify(obj: str) list[str][source]¶
- listify(obj: Iterable[T]) list[T]
- listify(obj: T) list[T]
Converts obj to a list intelligently.
Examples
>>> listify("str") ['str'] >>> listify((1, 2)) [1, 2] >>> listify(len) [<built-in function len>]
- make_even(iterable_1, iterable_2)[source]¶
- Extends the shorter of the two iterables with duplicate values until its
length is equal to the longer iterable (favours earlier elements).
See also
make_even_by_cyclingcycles elements instead of favouring earlier ones
Examples
>>> make_even([1, 2], [3, 4, 5, 6]) ([1, 1, 2, 2], [3, 4, 5, 6]) >>> make_even([1, 2], [3, 4, 5, 6, 7]) ([1, 1, 1, 2, 2], [3, 4, 5, 6, 7])
- make_even_by_cycling(iterable_1, iterable_2)[source]¶
- Extends the shorter of the two iterables with duplicate values until its
length is equal to the longer iterable (cycles over shorter iterable).
See also
make_evenfavours earlier elements instead of cycling them
Examples
>>> make_even_by_cycling([1, 2], [3, 4, 5, 6]) ([1, 2, 1, 2], [3, 4, 5, 6]) >>> make_even_by_cycling([1, 2], [3, 4, 5, 6, 7]) ([1, 2, 1, 2, 1], [3, 4, 5, 6, 7])
- remove_list_redundancies(lst)[source]¶
Used instead of
list(set(l))to maintain order. Keeps the last occurrence of each element.
- remove_nones(sequence)[source]¶
Removes elements where bool(x) evaluates to False.
Examples
>>> remove_nones(["m", "", "l", 0, 42, False, True]) ['m', 'l', 42, True]
- resize_array(nparray, length)[source]¶
- Extends/truncates nparray so that
len(result) == length. The elements of nparray are cycled to achieve the desired length.
See also
resize_preserving_orderfavours earlier elements instead of cycling them
make_even_by_cyclingsimilar cycling behaviour for balancing 2 iterables
Examples
>>> points = np.array([[1, 2], [3, 4]]) >>> resize_array(points, 1) array([[1, 2]]) >>> resize_array(points, 3) array([[1, 2], [3, 4], [1, 2]]) >>> resize_array(points, 2) array([[1, 2], [3, 4]])
- Extends/truncates nparray so that
- resize_preserving_order(nparray, length)[source]¶
- Extends/truncates nparray so that
len(result) == length. The elements of nparray are duplicated to achieve the desired length (favours earlier elements).
Constructs a zeroes array of length if nparray is empty.
See also
resize_arraycycles elements instead of favouring earlier ones
make_evensimilar earlier-favouring behaviour for balancing 2 iterables
Examples
>>> resize_preserving_order(np.array([]), 5) array([0., 0., 0., 0., 0.]) >>> nparray = np.array([[1, 2], [3, 4]]) >>> resize_preserving_order(nparray, 1) array([[1, 2]]) >>> resize_preserving_order(nparray, 3) array([[1, 2], [1, 2], [3, 4]])
- Parameters:
nparray (npt.NDArray[np.float64])
length (int)
- Return type:
npt.NDArray[np.float64]
- Extends/truncates nparray so that
- resize_with_interpolation(nparray, length)[source]¶
- Extends/truncates nparray so that
len(result) == length. New elements are interpolated to achieve the desired length.
Note that if nparray’s length changes, its dtype may too (e.g. int -> float: see Examples)
See also
resize_arraycycles elements instead of interpolating
resize_preserving_orderfavours earlier elements instead of interpolating
Examples
>>> nparray = np.array([[1, 2], [3, 4]]) >>> resize_with_interpolation(nparray, 1) array([[1., 2.]]) >>> resize_with_interpolation(nparray, 4) array([[1. , 2. ], [1.66666667, 2.66666667], [2.33333333, 3.33333333], [3. , 4. ]]) >>> nparray = np.array([[[1, 2], [3, 4]]]) >>> nparray = np.array([[1, 2], [3, 4], [5, 6]]) >>> resize_with_interpolation(nparray, 4) array([[1. , 2. ], [2.33333333, 3.33333333], [3.66666667, 4.66666667], [5. , 6. ]]) >>> nparray = np.array([[1, 2], [3, 4], [1, 2]]) >>> resize_with_interpolation(nparray, 4) array([[1. , 2. ], [2.33333333, 3.33333333], [2.33333333, 3.33333333], [1. , 2. ]])
- Extends/truncates nparray so that
- tuplify(obj: str) tuple[str][source]¶
- tuplify(obj: Iterable[T]) tuple[T]
- tuplify(obj: T) tuple[T]
Converts obj to a tuple intelligently.
Examples
>>> tuplify("str") ('str',) >>> tuplify([1, 2]) (1, 2) >>> tuplify(len) (<built-in function len>,)