def iwindowed(iterable, n):
'''
Take successive n-tuples from an iterable using a sliding window
'''
# Take n copies of the iterable
iterables = tee(iterable, n)
# Advance each to the correct starting position
for step, it in enumerate(iterables):
for s in range(step):
next(it)
# Zip the modified iterables and yield the elements as a genreator
# NOTE: not using zip longest as we want to stop when we reach the end
for t in zip(*iterables):
yield t
评论列表
文章目录