iterator question

George Sakkis george.sakkis at gmail.com
Tue Sep 26 15:48:06 EDT 2006


Steve Holden wrote:

> George Sakkis wrote:
> > Neil Cerutti wrote:
> >
> >
> >>On 2006-09-26, Neal Becker <ndbecker2 at gmail.com> 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




More information about the Python-list mailing list