Would like some thoughts on a grouped iterator.

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Sep 5 04:46:05 EDT 2016


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.

class upchecker:
    def __init__(self):
	self.prev = None
    def __call__(self, arg):
	if self.last is None:
            self.prev = arg
            return True
        elif arg >= self.last:
            self.prev = arg
            return True
        else:
            self.prev = arg
	    return False

So the iterator I need --- I call it grouped --- in combination with
the above class would be used someting like:

for itr in grouped([8, 10, 13, 11, 2, 17, 5, 12, 7, 14, 4, 6, 15, 16, 19, 9, 0, 1, 3, 18], upchecker()):
    print list(itr)

and the result would be:

[8, 10, 13]
[11]
[2, 17]
[5, 12]
[7, 14]
[4, 6, 15, 16, 19]
[9]
[0, 1, 3, 18]

Anyone an idea how I best tackle this?




More information about the Python-list mailing list