This is an elegant solution for sequences, but it doesn't work for arbitrary iterables (which need not support slicing).
This is an elegant solution for sequences, but it doesn't work for arbitrary iterables (which need not support slicing). While this generalization might not be needed for language ngrams, the general problem of taking n items at a time from an iterable pops up in various places. Here's a generator that yields ngrams from an arbitrary iterable: from collections import deque from itertools import islice def ngram_generator(iterable, n): iterator = iter(iterable) d = deque(islice(iterator, n-1), maxlen=n) for item in iterator: d.append(item) yield tuple(d)
Posted by e271828 on Aug 18, 2014, 12:50 AM
Tags
Related issues
- some bike vendor told me that the biggest problem with regular to ebike conversion was that regular bike frames were not strong enough to...
- I think of this as the kiosk economy.
- What a goofy comment.
- There's lots of evidence for positive influences of Caffeine on the body (some negative as well), especially the brain.
- Compose support in WSLc
- [RFC] Roadmap to v2.0
Raise real-world problems, find people facing the same issue, and discuss solutions.9,000 issues0 comments
support (Other)
some bike vendor told me that the biggest problem with regular to ebike conversion was that regular bike frames were not strong enough to...
I think of this as the kiosk economy.
What a goofy comment.
There's lots of evidence for positive influences of Caffeine on the body (some negative as well), especially the brain.
Compose support in WSLc
[RFC] Roadmap to v2.0
[Feature]: Add TurboQuant Support for KV Cache Quantization
Support for AGENTS.md and .agents/skills/, the community has been asking since August 2025
This is an elegant solution for sequences, but it doesn't work for arbitrary iterables (which need not support slicing).
This is an elegant solution for sequences, but it doesn't work for arbitrary iterables (which need not support slicing). While this generalization might not be needed for language ngrams, the general problem of taking n items at a time from an iterable pops up in various places. Here's a generator that yields ngrams from an arbitrary iterable: from collections import deque from itertools import islice def ngram_generator(iterable, n): iterator = iter(iterable) d = deque(islice(iterator, n-1), maxlen=n) for item in iterator: d.append(item) yield tuple(d)
Comments (0)
No comments yet. Start the discussion.