python - lambda 可以有多个回报吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16674004/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:20:05  来源:igfitidea点击:

python - can lambda have more than one return

pythonlambdatuples

提问by Shengjie

I know lambda doesn't have a return expression. Normally

我知道 lambda 没有返回表达式。一般

def one_return(a):
    #logic is here
    c = a + 1
    return c

can be written:

可以写成:

lambda a : a + 1

How about write this one in a lambda function:

在 lambda 函数中写这个怎么样:

def two_returns(a, b):
    # logic is here
    c = a + 1
    d = b * 1
    return c, d

采纳答案by óscar López

Yes, it's possible. Because an expression such as this at the end of a function:

是的,这是可能的。因为在函数末尾有这样的表达式:

return a, b

Is equivalent to this:

相当于:

return (a, b)

And there, you're really returning a single value: a tuple which happens to have two elements. So it's ok to have a lambda return a tuple, because it's a single value:

在那里,您实际上返回了一个值:一个恰好有两个元素的元组。所以让 lambda 返回一个元组是可以的,因为它是一个单一的值:

lambda a, b: (a, b) # here the return is implicit

回答by Daniel Roseman

Sure:

当然:

lambda a, b: (a + 1, b * 1)

回答by mgilson

what about:

关于什么:

lambda a,b: (a+1,b*1)