iterator question
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Sep 26 15:18:55 EDT 2006
Neal Becker a écrit :
> 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)
>
Just for the fun (I'm sure someone already posted a far better solution):
def transform(seq, step):
return [tuple(seq[x: x+step]) \
for x in range(0, len(seq)-(step -1), step)]
More information about the Python-list
mailing list