Returning the argument from a list comprehension that's result satisfies <something>
It's a horrible t开发者_开发技巧itle, I know; the basic explanation of what I mean is:
y=min([func(x) for x in range(z)])
As is, y
contains the value of the output of func(x_min)
. (x_min
might not actually be the smaller value in the range).
Basically, I'm looking for a way to get x_min
in this generic case in a more pythonic way. Any ideas?
UPDATE: From SilentGhosts answer, (RTFM...), the equivalent of the above would be;
x_min=min(range(z),key=func)
Use key
argument:
min(iterable, key=func)
精彩评论