Cleaner idiom for text processing?

Michele Simionato michele.simionato at poste.it
Thu May 27 04:44:05 EDT 2004


Peter Otten <__peter__ at web.de> wrote in message news:<c91got$vpi$00$1 at news.t-online.com>...
> Yet another way to create the dictionary:
> 
> >>> import itertools
> >>> nv = iter("foo 1 bar 2 baz 3\n".split())
> >>> dict(itertools.izip(nv, nv))
>  {'baz': '3', 'foo': '1', 'bar': '2'}
> >>>
> 
> Peter

BTW, the name I have seem for this kind of things is chop:

>>> 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 ... )

    Michele Simionato



More information about the Python-list mailing list