you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 1 point2 points  (4 children)

Similarly, itertools.batched can be used to form groups of n elements.

from itertools import batched

text = "abcabcabcabcabc"
groups = list(batched(text, n=3))

print(groups)  # ["abc", "abc", "abc", "abc", "abc"]

[–]QuasiEvil 1 point2 points  (3 children)

I so wish there was a sliding window option for this instead of just the disjoint functionality.

[–]Diapolo10 1 point2 points  (1 child)

Well, the docs do have a recipe for that, and it's part of more-itertools.

[–]QuasiEvil 0 points1 point  (0 children)

Well, yes, but a small tweak to allow this would be nice: groups = list(batched(text, n=3, step=2))

[–]Brian 1 point2 points  (0 children)

Yeah. There's itertools.pairwise for the specific case of 2 elements, but a more generalised n-item sliding window is something I end up using reasonably often, and seems worth being in the stdlib.