[Python-ideas] [Python-Dev] minmax() function returning (minimum, maximum) tuple of a sequence

Paul Moore p.f.moore at gmail.com
Tue Oct 12 17:51:03 CEST 2010


On 11 October 2010 21:18, Tal Einat <taleinat at gmail.com> wrote:
> We recently needed exactly this -- to do several running calculations
> in parallel on an iterable. We avoided using co-routines and just
> created a RunningCalc class with a simple interface, and implemented
> various running calculations as sub-classes, e.g. min, max, average,
> variance, n-largest. This isn't very fast, but since generating the
> iterated values is computationally heavy, this is fast enough for our
> uses.
>
> Having a standard method to do this in Python, with implementations
> for common calculations in the stdlib, would have been nice.
>
> I wouldn't mind trying to work up a PEP for this, if there is support
> for the idea.

The "consumer" interface as described in
http://effbot.org/zone/consumer.htm sounds about right for this:

class Rmin(object):
    def __init__(self):
        self.running_min = None
    def feed(self, val):
        if self.running_min is None:
            self.running_min = val
        else:
            self.running_min = min(self.running_min, val)
    def close(self):
        pass

rmin = Rmin()
for val in iter:
    rmin.feed(val)
print rmin.running_min

Paul.



More information about the Python-ideas mailing list