Created
August 5, 2020 23:28
-
-
Save seahrh/5f6576959bebe6314c5b442ce2b33647 to your computer and use it in GitHub Desktop.
argmin, argmax in Python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterable, TypeVar | |
T = TypeVar("T") | |
def argmin(elements: Iterable[T]) -> int: | |
"""Returns first index of smallest element.""" | |
return min(enumerate(elements), key=lambda x: x[1])[0] | |
def argmax(elements: Iterable[T]) -> int: | |
"""Returns first index of largest element.""" | |
return max(enumerate(elements), key=lambda x: x[1])[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment