IssueRaiser

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

Raise real-world problems, find people facing the same issue, and discuss solutions.9,000 issues0 comments

support (Other)

This is an elegant solution for sequences, but it doesn't work for arbitrary iterables (which need not support slicing).

e271828Aug 18, 2014, 12:50 AM

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.

Comment on this issue