[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-dev
Subject:    [Python-Dev] Proposal for a new itertools function: iwindow
From:       Torsten Marek <shlomme () gmx ! net>
Date:       2006-05-25 17:42:49
Message-ID: 4475EC99.7050006 () gmx ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


Hi,

in the last time, I've found myself reimplementing a generator that provides a
sliding-window-view over a sequence, and I think this function is of a greater
usefullness, so that it might be included in itertools.

Basically, what the generator does it return all m consecutive elements from a
sequence [i0, i1, ... in]. It then returns [i0, i1, i2], [i1, i2, i3], ...
[in-2, in-1, in] (assuming that m = 3).
In code, it looks like this:

>>> list(iwindow(range(0,5), 3))
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]

This list can be generated by using izip(a, a[1:], ..., a[n:]), but typing all
the sequence arguments gets tedious. If a is not a sequence but a generator,
tee(...) and islice has to be used.

It might be possible that the windows should be padded, so that the sequence of
windows starts with [pad, pad, ..., i0] and ends with [in, pad, pad, ...]

>>> list(iwindow(range(0,5), 3, pad=True))
[[None, None, 0], [None, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, None],
[4, None, None]]

Additionally, the value used for padding can be specified. This makes the
argument list of this function rather long, but the last two arguments are
optional anyway:
iwindow(iterable, window_size=3, pad = False, padding_value = None)

Some open question remain:
- should iwindow return lists or tuples?
- what happens if the length of the iterable is smaller than the window size,
and no padding is specified? Is this an error? Should the generator return no
value at all or one window that is too small?


I've attached a Python implementation of this function. If the function is
deemed to be actually useful, I'd be happy to brush up my C and provide a C
implementation along with docs and tests.


best,

Torsten

PS: Please CC me, as I'm not subscribed to the list
-- 
Torsten Marek <shlomme@gmx.net>
ID: A244C858 -- FP: 1902 0002 5DFC 856B F146  894C 7CC5 451E A244 C858
Keyserver: subkeys.pgp.net



["iwindow.py" (application/x-python)]
["signature.asc" (application/pgp-signature)]

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/python-dev%40progressive-comp.com


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic