[Python-Dev] Proposal for a new itertools function: iwindow (original) (raw)
Torsten Marek shlomme at gmx.net
Thu May 25 19:42:49 CEST 2006
- Previous message: [Python-Dev] Cost-Free Slice into FromString constructors--Long
- Next message: [Python-Dev] Proposal for a new itertools function: iwindow
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 at gmx.net> ID: A244C858 -- FP: 1902 0002 5DFC 856B F146 894C 7CC5 451E A244 C858 Keyserver: subkeys.pgp.net
-------------- next part -------------- A non-text attachment was scrubbed... Name: iwindow.py Type: application/x-python Size: 666 bytes Desc: not available Url : http://mail.python.org/pipermail/python-dev/attachments/20060525/97d16655/attachment.bin -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 252 bytes Desc: OpenPGP digital signature Url : http://mail.python.org/pipermail/python-dev/attachments/20060525/97d16655/attachment.pgp
- Previous message: [Python-Dev] Cost-Free Slice into FromString constructors--Long
- Next message: [Python-Dev] Proposal for a new itertools function: iwindow
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]