[Python-Dev] Proposal for a new itertools function: iwindow
Nick Coghlan
ncoghlan at gmail.com
Sun May 28 12:40:22 CEST 2006
Raymond Hettinger wrote:
> No thanks. The resolution of this one was that windowing iterables is
> not a good idea. It is the natural province of sequences, not
> iterables. With sequences, it is a matter of using an index and
> offset. With iterables, there is a great deal of data shifting. Also
> note that some of the uses are subsumed by collections.deque().
Interesting - the old 'hammer-nail' tunnel vision strikes again, I guess.
So moving the question to a different part of the docs, would it make sense to
include a deque recipe showing how to use a deque in a generator to permit
windowing of an arbitrary iterator? Something like:
def window(iterable, window_len=2, window_step=1):
itr = iter(iterable)
step_range = xrange(window_step)
current_window = collections.deque(islice(itr, window_len))
while window_len == len(current_window):
yield current_window
for idx in step_range:
current_window.popleft()
current_window.extend(islice(itr, window_step))
Even if an application doesn't use the generator approach directly, it still
illustrates how to use a deque for windowing instead of as a FIFO queue or a
stack.
> The thought process was documented in a series of newsgroup postings:
> http://groups.google.com/group/comp.lang.python/msg/026da8f9eec4becf
Thanks for that reference - I was searching the wrong list, so I didn't find it.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list