On 30/08/2020 09:51, Barry Scott wrote:
>>> a.index(max(a))
1
Barry
This has the drawback of passing twice over the list. The following
doesn't, but the complexity somewhat makes Filipp's point:
>>> min((e, i) for i, e in enumerate(a))[1]
0
That is 4x slower then my code for 1,000,000 items.
Ha! Fair enough. In principle, however, passing over the list once only
is preferable. The trade-off would depend on the cost of doing
comparisons: cheap here while iteration is clearly costing a lot. In the
alternative, the second pass for index() is probably only doing pointer
comparison.
It will often be the case and is perhaps why we don't have this
function. That and its existence in various libraries e.g.
https://iteration-utilities.readthedocs.io/en/latest/generated/argmin.html#iteration_utilities.argmin
.
Passing over the list once is what we would do in a C implementation,
I'm sure. One solution is to add a counter in bltinmodule.c::min_max()).
Jeff
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/PKT4ZAFNNPPKTLHZ3XQLJZYM4FWIJC5P/
Code of Conduct: http://python.org/psf/codeofconduct/