jameh

l33t windowing function

Oct 20th, 2013
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. def window(seq, size, step=1):
  2.     # initialize iterators
  3.     iters = [iter(seq) for i in range(size)]
  4.     # stagger iterators (without yielding)
  5.     [next(iters[i]) for j in range(size) for i in range(-1, -j-1, -1)]
  6.     while(True):
  7.         yield [next(i) for i in iters]
  8.         # next line does nothing for step = 1 (skips iterations for step > 1)
  9.         [next(i) for i in iters for j in range(step-1)]
Advertisement
Add Comment
Please, Sign In to add comment