Search a sequence for its minimum and stop as soon as the lowest possible value is found

Paul Rubin no.email at nospam.invalid
Sat Jan 7 22:46:10 EST 2017


Jussi Piitulainen <jussi.piitulainen at helsinki.fi> writes:
>> Use itertools.takewhile
> How? It consumes the crucial stop element:

Oh yucch, you're right, it takes it from both sides.  How about this:

    from itertools import takewhile, islice
    def minabs(xs):
	a = iter(xs)
	m = min(map(abs,takewhile(lambda x: x!=0, a)))
	z = list(islice(a,1))
	if z: return 0
	return m



More information about the Python-list mailing list