On Fri, 20 Mar 2009, paul bedaride wrote:
I propose a new function for list for pack values of a list and sliding over them:
then we can do things like this: for i, j, k in pack(range(10), 3, partialend=False): print i, j, k
I propose this because i need a lot of times pack and slide function over list and this one combine the two in a generator way.
See the Python documentation for zip(): http://docs.python.org/library/functions.html#zip And this article in which somebody independently rediscovers the idea: http://drj11.wordpress.com/2009/01/28/my-python-dream-about-groups/ Summary: except for the "partialend" parameter, this can already be done in a single line. It is not for me to say whether this nevertheless would be useful as a library routine (if only perhaps to make it easy to specify "partialend" explicitly). It seems to me that sometimes one would want izip instead of zip. And I think you could get the effect of partialend=True in 2.6 by using izip_longest (except with an iterator result rather than a list).
def pack(l, size=2, slide=2, partialend=True): lenght = len(l) for p in range(0,lenght-size,slide): def packet(): for i in range(size): yield l[p+i] yield packet() p = p + slide if partialend or lenght-p == size: def packet(): for i in range(lenght-p): yield l[p+i] yield packet()
Isaac Morland CSCF Web Guru DC 2554C, x36650 WWW Software Specialist