Last active
December 1, 2022 19:20
-
-
Save Ogaday/c26eaa8bbf812b1792f1163417ba0ef6 to your computer and use it in GitHub Desktop.
Native Argmax
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
| """ | |
| A native implementation of argmax. | |
| https://towardsdatascience.com/there-is-no-argmax-function-for-python-list-cd0659b05e49 | |
| Tests:: | |
| pytest --doctest-modules -vvv argmax.py | |
| Linting:: | |
| mypy argmax | |
| """ | |
| from typing import Sequence | |
| def argmax(iterable: Sequence) -> int: | |
| """ | |
| Return the index of the largest item in an iterable. | |
| Parameters | |
| ---------- | |
| iterable : Sequence | |
| The object in which to find the largest item. | |
| Examples | |
| -------- | |
| >>> argmax([3, 31, 29]) | |
| 1 | |
| """ | |
| return max(range(len(iterable)), key=lambda i: iterable[i]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment