开发者

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜