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

Steve D'Aprano steve+python at pearwood.info
Sat Jan 7 05:56:18 EST 2017


On Sat, 7 Jan 2017 07:06 am, Wolfgang Maier wrote:

> On 1/6/2017 15:04, Peter Otten wrote:
[...]
>> How would you implement stopmin()?
>>
> 
> How about:
> 
> def stopmin (iterable, key, stop):
>      def take_until ():
>          for e in iterable:
>              yield e
>              if key(e) <= stop:
>                  break
>      return min(take_until(), key=key)

Heh, great minds think alike :-)




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.



More information about the Python-list mailing list