On Monday 05 September 2016 18:46, Antoon Pardon wrote:

> I need an interator that takes an already existing iterator and
> divides it into subiterators of items belonging together.
> 
> For instance take the following class, wich would check whether
> the argument is greater or equal to the previous argument.


These sorts of filtering jobs are often so simple that its hardly worth 
generalising them unless you have a whole lot of them.


def group_increasing(seq):
    it = iter(seq)
    accum = []
    try:
        last = next(it)
    except StopIterator:
        return
    accum.append(last)
    for item in it:
        if item >= last:
            accum.append(item)
        else:
            yield tuple(accum)
            accum = [item]
        last = item
    if accum:
        yield tuple(accum)


If you do need to generalise, using itertools.groupby is probably the right way 
to do it.




-- 
Steve

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to