lambda 中的布尔计算

发布于 2024-07-29 06:08:27 字数 215 浏览 3 评论 0原文

只是为了我自己的娱乐而使用工具,我想使用 lambda,因为我喜欢它。 我可以用 lambda 替换这个函数吗?

def isodd(number):
    if (number%2 == 0):
        return False
    else:
        return True

初级,是的。 但我有兴趣知道...

Just tooling around for my own amusement, and I want to use a lambda, because I feel like it. Can I replace this function with a lambda?

def isodd(number):
    if (number%2 == 0):
        return False
    else:
        return True

Elementary, yes. But I'm interested to know...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

病毒体 2024-08-05 06:08:27

如果您确实不需要某个函数,即使没有 lambda,您也可以替换它。 :)

(number % 2 != 0)

本身就是一个计算结果为 True 或 False 的表达式。 或者甚至更简单,

bool(number % 2)

您可以像这样简化:

if number % 2:
    print "Odd!"
else:
    print "Even!"

但是否可读可能取决于情人的眼睛。

And if you don't really need a function you can replace it even without a lambda. :)

(number % 2 != 0)

by itself is an expression that evaluates to True or False. Or even plainer,

bool(number % 2)

which you can simplify like so:

if number % 2:
    print "Odd!"
else:
    print "Even!"

But if that's readable or not is probably in the eye of the beholder.

偏闹i 2024-08-05 06:08:27
lambda num: num % 2 != 0
lambda num: num % 2 != 0
ゞ花落谁相伴 2024-08-05 06:08:27

是的你可以:

isodd = lambda x: x % 2 != 0

Yes you can:

isodd = lambda x: x % 2 != 0
残月升风 2024-08-05 06:08:27

其他人已经给了您涵盖您的具体情况的答复。 不过,一般来说,当您确实需要 if 语句时,可以使用条件表达式。 例如,如果您必须返回字符串 "False""True" 而不是布尔值,您可以这样做:

lambda num: "False" if num%2==0 else "True"

Python 语言参考中该表达式的定义如下:

表达式x if C else y首先计算C(而不是x); 如果C为true,则计算x并返回其值; 否则,将计算 y 并返回其值。

Others already gave you replies that cover your particular case. In general, however, when you actually need an if-statement, you can use the conditional expression. For example, if you'd have to return strings "False" and "True" rather than boolean values, you could do this:

lambda num: "False" if num%2==0 else "True"

The definition of this expression in Python language reference is as follows:

The expression x if C else y first evaluates C (not x); if C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

£烟消云散 2024-08-05 06:08:27

并且不要忘记,您可以使用简单的短路逻辑来模拟复杂的条件句子,利用“and”和“or”返回它们的一些元素(最后一个评估的元素)......例如,在本例中,假设您想要返回与 True 或 False 不同的内容

lambda x: x%2 and "Odd" or "Even"

And also don't forget that you can emulate complex conditional sentences with simple short-circuit logic, taking advantage that "and" and "or" return some of their ellements (the last one evaluated)... for example, in this case, supposing you'd want to return something different than True or False

lambda x: x%2 and "Odd" or "Even"
花开柳相依 2024-08-05 06:08:27
isodd = lambda number: number %2 != 0
isodd = lambda number: number %2 != 0
月亮邮递员 2024-08-05 06:08:27

isodd = lambda 数:(False, True)[number & 1]

isodd = lambda number: (False, True)[number & 1]

亢潮 2024-08-05 06:08:27

任何时候你看到自己写:

if (some condition):
    return True
else:
    return False

你应该用一行替换它:

return (some condition)

你的函数然后变成:

def isodd(number):
    return number % 2 != 0

你应该能够看到如何从那里到达其他人提供的 lambda 解决方案。

Any time you see yourself writing:

if (some condition):
    return True
else:
    return False

you should replace it with a single line:

return (some condition)

Your function then becomes:

def isodd(number):
    return number % 2 != 0

You should be able to see how to get from there to the lambda solution that others have provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文