有没有办法执行“if”在 python 的 lambda 中?
在Python 2.6中,我想做:
f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
这显然不是语法。是否可以在 lambda
中执行 if
,如果可以,该怎么做?
In Python 2.6, I want to do:
f = lambda x: if x==2 print x else raise Exception()
f(2) #should print "2"
f(3) #should throw an exception
This clearly isn't the syntax. Is it possible to perform an if
in lambda
and if so how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
您正在寻找的语法:
但是您不能在 lambda 中使用
print
或raise
。The syntax you're looking for:
But you can't use
print
orraise
in a lambda.你为什么不直接定义一个函数呢?
在这种情况下确实没有理由使用 lambda。
why don't you just define a function?
there really is no justification to use lambda in this case.
可能是我迄今为止写过的最糟糕的Python行:
如果x == 2你打印,
如果x!= 2你提出。
Probably the worst python line I've written so far:
If x == 2 you print,
if x != 2 you raise.
如果您确实想要这样做,您可以轻松地在 lambda 中引发异常。
这是个好主意吗?我的直觉一般是不要将错误报告放在 lambda 中;让它的值为 None 并在调用者中引发错误。不过,我不认为这本质上是邪恶的——我认为“y if x else z”语法本身更糟糕——只要确保你没有试图在 lambda 体中填充太多内容即可。
You can easily raise an exception in a lambda, if that's what you really want to do.
Is this a good idea? My instinct in general is to leave the error reporting out of lambdas; let it have a value of None and raise the error in the caller. I don't think this is inherently evil, though--I consider the "y if x else z" syntax itself worse--just make sure you're not trying to stuff too much into a lambda body.
请注意,您可以在 lambda 定义中使用多个 else...if 语句:
note you can use several else...if statements in your lambda definition:
Python 中的 Lambda 对您可以使用的内容有相当的限制。具体来说,正文中不能包含任何关键字(
and
、not
、or
等运算符除外)。所以,你不可能在你的例子中使用 lambda(因为你不能使用
raise
),但如果你愿意承认这一点......你可以使用:Lambdas in Python are fairly restrictive with regard to what you're allowed to use. Specifically, you can't have any keywords (except for operators like
and
,not
,or
, etc) in their body.So, there's no way you could use a lambda for your example (because you can't use
raise
), but if you're willing to concede on that… You could use:这段代码应该可以帮助你:
This snippet should help you:
如果您仍然想打印,可以导入未来的模块
If you still want to print you can import future module
您还可以使用逻辑运算符来获得类似条件的内容。
您可以在此处查看有关逻辑运算符的更多信息
You can also use Logical Operators to have something like a Conditional
You can see more about Logical Operators here
给定场景的解决方案是:
the solution for the given scenerio is:
您现在真正需要的是
按照您需要的方式调用该函数
what you need exactly is
now call the function the way you need
在 lambda 中执行 if 的一种简单方法是使用列表理解。
您不能在 lambda 中引发异常,但这是 Python 3.x 中执行类似于您的示例的操作的一种方式:
另一个示例:
如果 M 则返回 1,否则返回 0
An easy way to perform an if in lambda is by using list comprehension.
You can't raise an exception in lambda, but this is a way in Python 3.x to do something close to your example:
Another example:
return 1 if M otherwise 0
如果您使用 Python 3.x,这是解决方案!
Here's the solution if you use Python 3.x!
以下示例代码对我有用。不确定它是否与这个问题直接相关,但希望它在其他情况下有所帮助。
Following sample code works for me. Not sure if it directly relates to this question, but hope it helps in some other cases.
希望这对您有一点帮助,
您可以通过以下方式解决这个问题
Hope this will help a little
you can resolve this problem in the following way
可能值得考虑 np.where
it might be worth considering np.where