iterator question

N

Neal Becker

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

johnzenger

def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size
 
J

John Hicken

Neal said:
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)

How about this:

def transform(seq,n):
for k in xrange(0,len(seq),n):
yield tuple(seq[k:k+n])
 
G

George Sakkis

def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size

Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George
 
N

Neil Cerutti

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:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
 
G

George Sakkis

Neil said:
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:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)

That's not quite the same as the previous suggestions; if the last
tuple is shorter than n, it pads the last tuple with padvalue. The OP
didn't mention if he wants that or he'd rather have a shorter last
tuple.

George
 
B

Boris Borcic

Neal said:
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:
>>> trf = lambda iterable,n : zip(*[iter(iterable)]*n)
>>> trf(range(15),3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 14)]


note though that it will silently drop any remainder.

hth, bb
 
S

Steve Holden

George said:
Neil Cerutti wrote:

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:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)


That's not quite the same as the previous suggestions; if the last
tuple is shorter than n, it pads the last tuple with padvalue. The OP
didn't mention if he wants that or he'd rather have a shorter last
tuple.
In which case why not go in for a bit of requirements gold-plating and
add a keyword Boolean argument that allows you to specify which
behaviour you want. Or, alternatively, let the OP make sense of the
suggestions already made.

regards
Steve
 
R

Rob Williscroft

Neal Becker wrote in
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.
 
R

Rob Williscroft

Rob Williscroft wrote in 216.196.109.145 in comp.lang.python:
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)]

For bigger sequences:
import itertools as it
seq = range(11)
even = it.islice( seq, 0, None, 2 )
odd = it.islice( seq, 1, None, 2 )
zipped = it.izip( even, it.chain( odd, [None] ) )
list( zipped )
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9), (10, None)]


Rob.
 
B

Bruno Desthuilliers

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)]
 
G

George Sakkis

Steve said:
George said:
Neil Cerutti wrote:

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:

It turns out there's a itertools recipe to do this; the last one
in the itertools recipe book:

def grouper(n, iterable, padvalue=None):
"""
grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')

"""
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)


That's not quite the same as the previous suggestions; if the last
tuple is shorter than n, it pads the last tuple with padvalue. The OP
didn't mention if he wants that or he'd rather have a shorter last
tuple.
In which case why not go in for a bit of requirements gold-plating and
add a keyword Boolean argument that allows you to specify which
behaviour you want.

Ok, I'll bite. Here's an overgeneralized, itertools-infested
conglomerate of all the suggestions so far. Season to taste:

from itertools import islice,izip,chain,repeat,takewhile,count

def iterwindows(iterable, n=2, mode='keep', padvalue=None):
it = iter(iterable)
if mode == 'keep':
return takewhile(bool, (tuple(islice(it,n)) for _ in count()))
elif mode == 'drop':
return izip(*[it]*n)
elif mode == 'pad':
return izip(*[chain(it,repeat(padvalue,n-1))]*n)
else:
raise ValueError('Unknown mode: %r' % mode)
list(iterwindows('abcdefgh',3)) [('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h')]
list(iterwindows('abcdefgh',3,mode='drop'))
[('a', 'b', 'c'), ('d', 'e', 'f')]
list(iterwindows('abcdefgh',3,mode='pad'))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', None)]


George
 
N

Neal Becker

George said:
def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size

Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George

Thanks guys!

This one above is my personal favorite.
 
D

dannycolligan

Simple list comprehension?
l = [1,2,3,4,5,6,7,8,9,0]
[(x, x+1) for x in l if x%2 == 1]
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

Danny

Neal said:
George said:
def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size

Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George

Thanks guys!

This one above is my personal favorite.
 
D

dannycolligan

Er, whoops. That would work if the last item in the list was 10 (and,
of course, this doesn't work for any arbitrary sequence). Is there any
"look-ahead" function for list comprehensions?

Danny

Simple list comprehension?
l = [1,2,3,4,5,6,7,8,9,0]
[(x, x+1) for x in l if x%2 == 1]
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]

Danny

Neal said:
George said:
(e-mail address removed) wrote:

def transform(seq, size):
i = 0
while i < len(seq):
yield tuple(seq[i:i+size])
i += size

Or for arbitrary iterables, not just sequences:

from itertools import islice
def transform(iterable, size):
it = iter(iterable)
while True:
window = tuple(islice(it,size))
if not window:
break
yield window

George

Thanks guys!

This one above is my personal favorite.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top