Cleaner idiom for text processing?

Peter Otten __peter__ at web.de
Thu May 27 05:11:01 EDT 2004


Michele Simionato wrote:

>>>> import itertools
>>>> def chop(it, n):
> ...     tup = (iter(it),)*n
> ...     return itertools.izip(*tup)
> ...
>>>> list(chop([1,2,3,4,5,6],3))
> [(1, 2, 3), (4, 5, 6)]
>>>> list(chop([1,2,3,4,5,6],2))
> [(1, 2), (3, 4), (5, 6)]
>>>> list(chop([1,2,3,4,5,6],1))
> [(1,), (2,), (3,), (4,), (5,), (6,)]
> 
> (I don't remember if this is already in itertools ... )

I don't think so. IMO the itertools examples page would be a better place
for the above than the cookbook. If an example had to be removed in
exchange, that would be iteritems(). Raymond, are you looking?

Peter




More information about the Python-list mailing list