a sequence question
Nick Coghlan
ncoghlan at iinet.net.au
Fri Feb 11 08:07:25 EST 2005
David Isaac wrote:
> "Nick Coghlan" <ncoghlan at iinet.net.au> wrote in message
> news:mailman.1553.1106960946.22381.python-list at python.org...
>
>>Using zip(*[iter(l)]*N) or zip(*(iter(l),)*N) simply extends the above to
>
> the
>
>>general case.
>
>
> Clearly true.
> But can you please go into much more detail for a newbie?
> I see that [iter(l)]*N produces an N element list with each element being
> the same iterator object, but after that
> http://www.python.org/doc/2.3.5/lib/built-in-funcs.html
> just didn't get me there.
See if the following interactive examples clear things up at all:
# The unclear version
Py> itr = iter(range(10))
Py> zipped = zip(*(itr,)*3) # How does this bit work?
Py> print "\n".join(map(str, zipped))
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
# Manual zip, printing as we go
Py> itr = iter(range(10))
Py> try:
... while 1: print (itr.next(), itr.next(), itr.next())
... except StopIteration:
... pass
...
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
# Manual zip, actually behaving somewhat like the real thing
Py> itr = iter(range(10))
Py> zipped = []
Py> try:
... while 1: zipped.append((itr.next(), itr.next(), itr.next()))
... except StopIteration:
... pass
...
Py> print "\n".join(map(str, zipped))
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-list
mailing list