Index of maximum element in list
Paul Rubin
http
Sat Jan 26 15:40:26 EST 2008
bearophileHUGS at lycos.com writes:
> > def posmax(seq, key=lambda x:x):
> > return max(enumerate(seq), key=lambda k: key(k[1]))[0]
>
> Is the Python max able to tell that's the identity function? I don't
> think so, so your version may be slower and more memory hungry in the
> common situation where you don't have a key function. So I think my
> version is better :-)
I don't think there will be a noticable memory increase. It's not a
DSU situation like in sorting, it's just a small constant parameter.
Yes there might be a small speed hit. The compiler in principle could
convert the (lambda k: lambda x: k[1]) to something like
operator.itemgetter(1), but I doubt that it actually does so.
More information about the Python-list
mailing list