Hello All, Do you think there is a place for an "argmax" function (which I use a lot) in the standard library? I'm thinking in the lines of: def argmax(items, func, cmp=cmp): '''argmax(items, func) -> max_item, max_value''' it = iter(items) # Initial values try: item = it.next() except StopIteration: raise ValueError("can't run over empty sequence") val = func(item) for i in it: v = func(i) if cmp(v, val) == 1: item = i val = v return item, val def argmin(items, func, cmp=cmp): '''argmin(items, func) -> min_item, min_value''' return argmax(items, func, lambda x,y : -cmp(x,y)) Thanks. -- ----------------------------------------------------------- Smile, damn it, smile. lambda msg: { "name" : "Miki Tebeka", "email" : "miki.tebeka at zoran.com", "url" : "http://www.cs.bgu.ac.il/~tebeka", "quote" : "The only difference between children and adults " \ "is the price of the toys" }[msg]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4