iterables#
Operations on iterables.
Functions
- adjacent_n_tuples(objects, n)[source]#
Returns the Sequence objects cyclically split into n length tuples.
See also
adjacent_pairs
alias with n=2
Examples
Normal usage:
list(adjacent_n_tuples([1, 2, 3, 4], 2)) # returns [(1, 2), (2, 3), (3, 4), (4, 1)] list(adjacent_n_tuples([1, 2, 3, 4], 3)) # returns [(1, 2, 3), (2, 3, 4), (3, 4, 1), (4, 1, 2)]
- Parameters
objects (Sequence) –
n (int) –
- Return type
zip
- adjacent_pairs(objects)[source]#
Alias for
adjacent_n_tuples(objects, 2)
.See also
Examples
Normal usage:
list(adjacent_pairs([1, 2, 3, 4])) # returns [(1, 2), (2, 3), (3, 4), (4, 1)]
- Parameters
objects (Sequence) –
- Return type
zip
- all_elements_are_instances(iterable, Class)[source]#
Returns
True
if all elements of iterable are instances of Class. False otherwise.- Parameters
iterable (Iterable) –
- 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
Normal usage:
batch_by_property([(1, 2), (3, 4), (5, 6, 7), (8, 9)], len) # returns [([(1, 2), (3, 4)], 2), ([(5, 6, 7)], 3), ([(8, 9)], 2)]
- Parameters
items (Sequence) –
property_func (Callable) –
- Return type
list[tuple[list, Any]]
- concatenate_lists(*list_of_lists)[source]#
Combines the Iterables provided as arguments into one list.
Examples
Normal usage:
concatenate_lists([1, 2], [3, 4], [5]) # returns [1, 2, 3, 4, 5]
- Parameters
list_of_lists (Iterable) –
- Return type
list
- 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
Normal usage:
list_difference_update([1, 2, 3, 4], [2, 4]) # returns [1, 3]
- Parameters
l1 (Iterable) –
l2 (Iterable) –
- Return type
list
- 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
Normal usage:
list_update([1, 2, 3], [2, 4, 4]) # returns [1, 3, 2, 4, 4]
- Parameters
l1 (Iterable) –
l2 (Iterable) –
- Return type
list
- Used instead of
- listify(obj)[source]#
Converts obj to a list intelligently.
Examples
Normal usage:
listify('str') # ['str'] listify((1, 2)) # [1, 2] listify(len) # [<built-in function len>]
- Return type
list
- 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_cycling
cycles elements instead of favouring earlier ones
Examples
Normal usage:
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])
- Parameters
iterable_1 (Iterable) –
iterable_2 (Iterable) –
- Return type
tuple[list, list]
- 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_even
favours earlier elements instead of cycling them
Examples
Normal usage:
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])
- Parameters
iterable_1 (Collection) –
iterable_2 (Collection) –
- Return type
tuple[list, list]
- remove_list_redundancies(lst)[source]#
Used instead of
list(set(l))
to maintain order. Keeps the last occurrence of each element.- Parameters
lst (Reversible) –
- Return type
list
- remove_nones(sequence)[source]#
Removes elements where bool(x) evaluates to False.
Examples
Normal usage:
remove_nones(['m', '', 'l', 0, 42, False, True]) # ['m', 'l', 42, True]
- Parameters
sequence (Iterable) –
- Return type
list
- 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_order
favours earlier elements instead of cycling them
make_even_by_cycling
similar cycling behaviour for balancing 2 iterables
Examples
Normal usage:
>>> 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]])
- Parameters
nparray (ndarray) –
length (int) –
- Return type
ndarray
- 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_array
cycles elements instead of favouring earlier ones
make_even
similar earlier-favouring behaviour for balancing 2 iterables
Examples
Normal usage:
resize_preserving_order(np.array([]), 5) # np.array([0., 0., 0., 0., 0.]) nparray = np.array([[1, 2], [3, 4]]) resize_preserving_order(nparray, 1) # np.array([[1, 2]]) resize_preserving_order(nparray, 3) # np.array([[1, 2], # [1, 2], # [3, 4]])
- Parameters
nparray (ndarray) –
length (int) –
- Return type
ndarray
- 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_array
cycles elements instead of interpolating
resize_preserving_order
favours earlier elements instead of interpolating
Examples
Normal usage:
nparray = np.array([[1, 2], [3, 4]]) resize_with_interpolation(nparray, 1) # np.array([[1., 2.]]) resize_with_interpolation(nparray, 4) # np.array([[1. , 2. ], # [1.66666667, 2.66666667], # [2.33333333, 3.33333333], # [3. , 4. ]]) nparray = np.array([[[1, 2],[3, 4]]]) resize_with_interpolation(nparray, 3) # np.array([[[1., 2.], [3., 4.]], # [[1., 2.], [3., 4.]], # [[1., 2.], [3., 4.]]]) nparray = np.array([[1, 2], [3, 4], [5, 6]]) resize_with_interpolation(nparray, 4) # np.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) # np.array([[1. , 2. ], # [2.33333333, 3.33333333], # [2.33333333, 3.33333333], # [1. , 2. ]])
- Parameters
nparray (ndarray) –
length (int) –
- Return type
ndarray
- Extends/truncates nparray so that
- stretch_array_to_length(nparray, length)[source]#
- Parameters
nparray (ndarray) –
length (int) –
- Return type
ndarray