python中一行lambda函数中的条件语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15772617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conditional statement in a one line lambda function in python?
提问by Nathan Bush
Apologies if this has been asked before, but I couldn't see it anywhere.
抱歉,如果之前有人问过这个问题,但我在任何地方都看不到。
Essentially I've come across a scenario where i need to make use of an if statement inside a lambda function. What makes it difficult is that ideally it needs to be in a single line of code (if thats even possible?)
本质上,我遇到过一个场景,我需要在 lambda 函数中使用 if 语句。让它变得困难的是,理想情况下它需要在一行代码中(如果可能的话?)
Normally, i would write this:
通常,我会这样写:
T = 250
if (T > 200):
rate = 200*exp(-T)
else:
rate = 400*exp(-T)
return (rate)
However i need it to look like this:
但是我需要它看起来像这样:
rate = lambda(T) : if (T>200): return(200*exp(-T)); else: return(400*exp(-T))
I realize the easier thing to do would to take the decision making outside of the lambda functions, and then have a separate lambda function for each case, but its not really suitable here. The lambda functions are stored in an array and accessed when needed, with each array element corresponding to a particular "rate" so having two separate rows for the same "rate" would mess things up. Any help would be greatly appreciated, or if its not possible, some confirmation from others would be nice :)
我意识到更容易做的事情是在 lambda 函数之外做出决策,然后为每种情况设置一个单独的 lambda 函数,但它在这里并不合适。lambda 函数存储在一个数组中并在需要时访问,每个数组元素对应于一个特定的“速率”,因此对于相同的“速率”有两个单独的行会使事情变得混乱。任何帮助将不胜感激,或者如果不可能,其他人的一些确认会很好:)
回答by shx2
Use the exp1 if cond else exp2syntax.
使用exp1 if cond else exp2语法。
rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)
Note you don't use returnin lambda expressions.
请注意,您不在returnlambda 表达式中使用。
回答by Silas Ray
Yes, you can use the shorthand syntax for ifstatements.
是的,您可以对if语句使用速记语法。
rate = lambda(t): (200 * exp(-t)) if t > 200 else (400 * exp(-t))
Note that you don't use explicit returnstatements inlambdas either.
请注意,您也不return在lambdas 中使用显式语句。
回答by abarnert
The right way to do this is simple:
正确的方法很简单:
def rate(T):
if (T > 200):
return 200*exp(-T)
else:
return 400*exp(-T)
There is absolutely no advantage to using lambdahere. The only thing lambdais good for is allowing you to create anonymous functions and use them in an expression (as opposed to a statement). If you immediately assign the lambdato a variable, it's no longer anonymous, and it's used in a statement, so you're just making your code less readable for no reason.
在lambda这里使用绝对没有任何好处。唯一的lambda好处是允许您创建匿名函数并在表达式中使用它们(而不是语句)。如果您立即将 分配lambda给一个变量,它就不再是匿名的,而是在语句中使用,因此您只会无缘无故地降低代码的可读性。
The ratefunction defined this way can be stored in an array, passed around, called, etc. in exactly the same way a lambda function could. It'll be exactly the same (except a bit easier to debug, introspect, etc.).
以rate这种方式定义的函数可以以与 lambda 函数完全相同的方式存储在数组中、传递、调用等。它将完全相同(除了更容易调试、内省等)。
From a comment:
来自评论:
Well the function needed to fit in one line, which i didn't think you could do with a named function?
好吧,函数需要放在一行中,我认为你不能用命名函数来做到这一点?
I can't imagine any good reason why the function would ever need to fit in one line. But sure, you can do that with a named function. Try this in your interpreter:
我无法想象为什么该函数需要放在一行中的任何充分理由。但是可以肯定的是,您可以使用命名函数来做到这一点。在你的解释器中试试这个:
>>> def foo(x): return x + 1
Also these functions are stored as strings which are then evaluated using "eval" which i wasn't sure how to do with regular functions.
这些函数也存储为字符串,然后使用“eval”进行评估,我不确定如何使用常规函数。
Again, while it's hard to be 100% sure without any clue as to why why you're doing this, I'm at least 99% sure that you have no reason or a bad reason for this. Almost any time you think you want to pass Python functions around as strings and call evalso you can use them, you actually just want to pass Python functions around as functions and use them as functions.
同样,虽然在不知道为什么这样做的情况下很难 100% 确定,但我至少 99% 确定您没有理由或理由不充分。几乎任何时候您认为您想将 Python 函数作为字符串传递并调用eval以便您可以使用它们,实际上您只想将 Python 函数作为函数传递并作为函数使用它们。
But on the off chance that this really is what you need here: Just use execinstead of eval.
但是,如果这确实是您在这里需要的可能性很小:只需使用exec而不是eval.
You didn't mention which version of Python you're using. In 3.x, the execfunction has the exact same signature as the evalfunction:
您没有提到您使用的是哪个版本的 Python。在 3.x 中,该exec函数具有与函数完全相同的签名eval:
exec(my_function_string, my_globals, my_locals)
In 2.7, execis a statement, not a function—but you can still write it in the same syntax as in 3.x (as long as you don't try to assign the return value to anything) and it works.
在 2.7 中,exec是一个语句,而不是一个函数——但你仍然可以用与 3.x 中相同的语法来编写它(只要你不尝试将返回值分配给任何东西)并且它可以工作。
In earlier 2.x (before 2.6, I think?) you have to do it like this instead:
在早期的 2.x(我认为是 2.6 之前?)你必须这样做:
exec my_function_string in my_globals, my_locals
回答by tdelaney
By the time you say rate = lambda whatever...you've defeated the point of lambda and should just define a function. But, if you want a lambda, you can use 'and' and 'or'
当你说rate = lambda whatever...你已经打败了 lambda 的要点并且应该定义一个函数时。但是,如果你想要一个 lambda,你可以使用 'and' 和 'or'
lambda(T): (T>200) and (200*exp(-T)) or (400*exp(-T))
回答by pkmccroskey
I found I COULD use "if-then" statements in a lambda. For instance:
我发现我可以在 lambda 中使用“if-then”语句。例如:
eval_op = {
'|' : lambda x,y: eval(y) if (eval(x)==0) else eval(x),
'&' : lambda x,y: 0 if (eval(x)==0) else eval(y),
'<' : lambda x,y: 1 if (eval(x)<eval(y)) else 0,
'>' : lambda x,y: 1 if (eval(x)>eval(y)) else 0,
}

