iterator question

Rob Williscroft rtw at freenet.co.uk
Tue Sep 26 15:12:51 EDT 2006


Neal Becker wrote in
news:mailman.716.1159290541.10491.python-list at python.org in
comp.lang.python: 

> Any suggestions for transforming the sequence:
> 
> [1, 2, 3, 4...]
> Where 1,2,3.. are it the ith item in an arbitrary sequence
> 
> into a succession of tuples:
> 
> [(1, 2), (3, 4)...]
> 
> In other words, given a seq and an integer that specifies the size of
> tuple to return, then for example:
> 
> seq = [a,b,c,d,e,f]
> for e in transform (seq, 2):
>   print e
> 
> would return
> (a,b)
> (c,d)
> (e,f)
> 

>>> seq = range(11)
>>> zip(seq[::2], seq[1::2] + [None])
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)]

>>> seq = range(10)
>>> zip(seq[::2], seq[1::2] + [None])
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]


Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list