This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --===============0214719841== Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="------------enig5E96D9B18587B6B117AB7917" This is an OpenPGP/MIME signed message (RFC 2440 and 3156) --------------enig5E96D9B18587B6B117AB7917 Content-Type: multipart/mixed; boundary="------------070902020003000200010008" This is a multi-part message in MIME format. --------------070902020003000200010008 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, in the last time, I've found myself reimplementing a generator that provi= des a sliding-window-view over a sequence, and I think this function is of a gr= eater usefullness, so that it might be included in itertools. Basically, what the generator does it return all m consecutive elements f= rom a sequence [i0, i1, ... in]. It then returns [i0, i1, i2], [i1, i2, i3], ..= =2E [in-2, in-1, in] (assuming that m =3D 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 typin= g all the sequence arguments gets tedious. If a is not a sequence but a generat= or, tee(...) and islice has to be used. It might be possible that the windows should be padded, so that the seque= nce of windows starts with [pad, pad, ..., i0] and ends with [in, pad, pad, ...]= >>> list(iwindow(range(0,5), 3, pad=3DTrue)) [[None, None, 0], [None, 0, 1], [0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, N= one], [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 ar= e optional anyway: iwindow(iterable, window_size=3D3, pad =3D False, padding_value =3D None)= Some open question remain: - should iwindow return lists or tuples? - what happens if the length of the iterable is smaller than the window s= ize, and no padding is specified? Is this an error? Should the generator retur= n no value at all or one window that is too small? I've attached a Python implementation of this function. If the function i= s 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 --=20 Torsten Marek ID: A244C858 -- FP: 1902 0002 5DFC 856B F146 894C 7CC5 451E A244 C858 Keyserver: subkeys.pgp.net --------------070902020003000200010008 Content-Type: application/x-python; name="iwindow.py" Content-Transfer-Encoding: base64 Content-Disposition: inline; filename="iwindow.py" ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgZGVxdWUKZnJvbSBpdGVydG9vbHMgaW1wb3J0IGlz bGljZSwgY2hhaW4KCmRlZiBpd2luZG93KGl0ZXJhYmxlLCB3aW5kb3dfc2l6ZT0zLCBwYWQg PSBGYWxzZSwgcGFkZGluZ192YWx1ZSA9IE5vbmUpOgogICAgIiIiaXdpbmRvdyhpdGVyYWJs ZSwgd2luZG93X3NpemU9MywgcGFkID0gRmFsc2UsIHBhZGRpbmdfdmFsdWUgPSBOb25lKSAt LT4gaXdpbmRvdyBvYmplY3QKICAgICIiIgoKICAgIGlmIHBhZDoKICAgICAgICByZWFsX2l0 ZXIgPSBjaGFpbihpdGVyKGl0ZXJhYmxlKSwgW3BhZGRpbmdfdmFsdWVdICogKHdpbmRvd19z aXplIC0gMSkpCiAgICAgICAgdyA9IGRlcXVlKFtwYWRkaW5nX3ZhbHVlXSAqIHdpbmRvd19z aXplKQogICAgZWxzZToKICAgICAgICByZWFsX2l0ZXIgPSBpdGVyKGl0ZXJhYmxlKQogICAg ICAgIHcgPSBkZXF1ZShpc2xpY2UocmVhbF9pdGVyLCB3aW5kb3dfc2l6ZSAtIDEpKQogICAg ICAgIHcuYXBwZW5kbGVmdChOb25lKQogICAgICAgIGlmIGxlbih3KSA8IHdpbmRvd19zaXpl OgogICAgICAgICAgICByZXR1cm4KICAgICAgICAKICAgIGZvciBpdGVtIGluIHJlYWxfaXRl cjoKICAgICAgICB3LnJvdGF0ZSgtMSkKICAgICAgICB3Wy0xXSA9IGl0ZW0KICAgICAgICB5 aWVsZCBsaXN0KHcpCgogICAgCg== --------------070902020003000200010008-- --------------enig5E96D9B18587B6B117AB7917 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFEdeyvfMVFHqJEyFgRAk3nAJ9wHSvbD18/G20xsH3xJx6QsYj0dQCggVMA IFXCYuH2/wjjhucE6tC5oSI= =6bNh -----END PGP SIGNATURE----- --------------enig5E96D9B18587B6B117AB7917-- --===============0214719841== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ 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 --===============0214719841==--