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. 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() a = range(10) print a print 'pack(a, 2, 2, True):', [list(p) for p in pack(a, 2, 2, True)] print 'pack(a, 2, 2, False):', [list(p) for p in pack(a, 2, 2, False)] print 'pack(a, 2, 3, True):', [list(p) for p in pack(a, 2, 3, True)] print 'pack(a, 2, 3, False):', [list(p) for p in pack(a, 2, 3, False)] print 'pack(a, 3, 2, True):', [list(p) for p in pack(a, 3, 2, True)] print 'pack(a, 3, 2, False):', [list(p) for p in pack(a, 3, 2, False)] print 'pack(a, 3, 3, True):', [list(p) for p in pack(a, 3, 3, True)] print 'pack(a, 3, 3, False):', [list(p) for p in pack(a, 3, 3, False)] paul bedaride
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
On Fri, Mar 20, 2009, paul bedaride wrote:
I propose a new function for list for pack values of a list and sliding over them:
Please switch this discussion to python-ideas -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "Programming language design is not a rational science. Most reasoning about it is at best rationalization of gut feelings, and at worst plain wrong." --GvR, python-ideas, 2009-3-1
participants (3)
-
Aahz
-
Isaac Morland
-
paul bedaride