bff.sliding_window

bff.sliding_window(sequence, window_size, step)

Apply a sliding window over the sequence.

Each window is yielded. If there is a remainder, the remainder is yielded last, and will be smaller than the other windows.

Parameters
  • sequence (Sequence) – Sequence to apply the sliding window on (can be str, list, numpy.array, etc.).

  • window_size (int) – Size of the window to apply on the sequence.

  • step (int) – Step for each sliding window.

Yields

Sequence – Sequence generated.

Examples

>>> list(sliding_window('abcdef', 2, 1))
['ab', 'bc', 'cd', 'de', 'ef']
>>> list(sliding_window(np.array([1, 2, 3, 4, 5, 6]), 5, 5))
[array([1, 2, 3, 4, 5]), array([6])]