lkj.chunking#
Tools for chunking (segumentation, batching, slicing, etc.)
Functions
|
Divide an iterable into chunks/batches of a specific size. |
|
Chunks an iterable into non-overlapping chunks of size |
- lkj.chunking.chunk_iterable(iterable, chk_size, *, chunk_type=None)[source]#
Divide an iterable into chunks/batches of a specific size.
Handles both mappings (e.g. dicts) and non-mappings (lists, tuples, sets…) as you probably expect it to (if you give a dict input, it will chunk on the (key, value) items and return dicts of these). Thought note that you always can control the type of the chunks with the
chunk_typeargument.- Parameters:
- Return type:
Iterator[list[TypeVar(T)] |tuple[TypeVar(T),...] |dict[TypeVar(KT),TypeVar(VT)]]- Returns:
An iterator of dicts if the input is a Mapping, otherwise an iterator of collections (list, tuple, set…).
Examples
>>> list(chunk_iterable([1, 2, 3, 4, 5], 2)) [[1, 2], [3, 4], [5]]
>>> list(chunk_iterable((1, 2, 3, 4, 5), 3, chunk_type=tuple)) [(1, 2, 3), (4, 5)]
>>> list(chunk_iterable({"a": 1, "b": 2, "c": 3}, 2)) [{'a': 1, 'b': 2}, {'c': 3}]
>>> list(chunk_iterable({"x": 1, "y": 2, "z": 3}, 1, chunk_type=dict)) [{'x': 1}, {'y': 2}, {'z': 3}]
- lkj.chunking.chunker(a, chk_size, *, include_tail=True)[source]#
Chunks an iterable into non-overlapping chunks of size
chk_size.Note
This chunker is simpler, but also less efficient than
chunk_iterable. It does have the extrainclude_tailargument, though. Though note that you can get the effect ofinclude_tail=Falseinchunk_iterableby usingfilter(lambda x: len(x) == chk_size, chunk_iterable(...)).- Parameters:
- Return type:
- Returns:
An iterator of tuples, where each tuple is a chunk of size
chk_size(or fewer elements ifinclude_tailis True).
Examples
>>> list(chunker(range(8), 3)) [(0, 1, 2), (3, 4, 5), (6, 7)] >>> list(chunker(range(8), 3, include_tail=False)) [(0, 1, 2), (3, 4, 5)]