python中一行lambda函数中的条件语句?

时间:2021-09-26 19:10:43

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函数之外做出决策,然后为每个case都有一个单独的lambda函数,但它不适合这里。 lambda函数存储在一个数组中,并在需要时访问,每个数组元素对应一个特定的“速率”,因此对于相同的“速率”有两个单独的行会弄乱。任何帮助将不胜感激,或如果它不可能,其他人的一些确认将是不错的:)

5 个解决方案

#1


61  

Use the exp1 if cond else exp2 syntax.

使用exp1 if cond else exp2语法。

rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)

Note you don't use return in lambda expressions.

请注意,不要在lambda表达式中使用return。

#2


17  

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 lambda here. The only thing lambda is 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 lambda to 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 rate function 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.).

以这种方式定义的速率函数可以以与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 eval so 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 exec instead of eval.

但是在这里你真正需要的机会就是:只需使用exec而不是eval。

You didn't mention which version of Python you're using. In 3.x, the exec function has the exact same signature as the eval function:

您没有提到您正在使用的Python版本。在3.x中,exec函数与eval函数具有完全相同的签名:

exec(my_function_string, my_globals, my_locals)

In 2.7, exec is 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

#3


7  

Yes, you can use the shorthand syntax for if statements.

是的,您可以使用if语句的简写语法。

rate = lambda(t): (200 * exp(-t)) if t > 200 else (400 * exp(-t))

Note that you don't use explicit return statements inlambdas either.

请注意,您也不要在inlambdas中使用显式返回语句。

#4


3  

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时......你已经击败了lambda的点并且应该只定义一个函数。但是,如果你想要一个lambda,你可以使用'和'和'或'

lambda(T): (T>200) and (200*exp(-T)) or (400*exp(-T))

#5


2  

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,
}

#1


61  

Use the exp1 if cond else exp2 syntax.

使用exp1 if cond else exp2语法。

rate = lambda T: 200*exp(-T) if T>200 else 400*exp(-T)

Note you don't use return in lambda expressions.

请注意,不要在lambda表达式中使用return。

#2


17  

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 lambda here. The only thing lambda is 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 lambda to 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 rate function 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.).

以这种方式定义的速率函数可以以与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 eval so 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 exec instead of eval.

但是在这里你真正需要的机会就是:只需使用exec而不是eval。

You didn't mention which version of Python you're using. In 3.x, the exec function has the exact same signature as the eval function:

您没有提到您正在使用的Python版本。在3.x中,exec函数与eval函数具有完全相同的签名:

exec(my_function_string, my_globals, my_locals)

In 2.7, exec is 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

#3


7  

Yes, you can use the shorthand syntax for if statements.

是的,您可以使用if语句的简写语法。

rate = lambda(t): (200 * exp(-t)) if t > 200 else (400 * exp(-t))

Note that you don't use explicit return statements inlambdas either.

请注意,您也不要在inlambdas中使用显式返回语句。

#4


3  

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时......你已经击败了lambda的点并且应该只定义一个函数。但是,如果你想要一个lambda,你可以使用'和'和'或'

lambda(T): (T>200) and (200*exp(-T)) or (400*exp(-T))

#5


2  

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,
}