N-grams

Paul Rubin no.email at nospam.invalid
Thu Nov 10 02:41:45 EST 2016


Ian Kelly <ian.g.kelly at gmail.com> writes:
> I'd use the maxlen argument to deque here.

Oh that's cool, it's a Python 3 thing though.

> Better to move the extra yield above the loop and reorder the loop
> body so that the yielded tuple includes the element just read.

Thanks, I'll give that a try.

>>         if len(d) == n:
> As written, I don't see how this would ever be false.

Yes you're right, I should have left that out, I noticed it after
posting.

There's another bug too, I think, if the length of the initial iterable
is exactly n.  I better test that.  So this was at best ok as a sketch
of the deque approach, not something to actually use ;)

> What makes this better than the tee version?

Doesn't need all those separate tees, which per Srinivas Devaki would
use quadratic space.



More information about the Python-list mailing list