英文:
How to convert function to lambda
问题
我的函数如下:
def majority(x):
if x > 17:
return True
return False
示例lambda伪代码:
majority = lambda x: True if x > 17 else False
英文:
My function is below
def majority(x):
if x > 17:
return True
return False
Sample lambda pseudo-code
majority = lambda x: if x > 17 return True ? False
答案1
得分: 8
你需要提供一个表达式。你可以使用Python的“条件表达式”来实现这个。
majority = lambda x: True if x > 17 else False
然而,这可以简化为
majority = lambda x: x > 17
条件表达式的形式如下:
如果条件成立则为真值 else 如果条件不成立则为假值
英文:
You need to provide an expression. You can do this with python's "conditional expressions"
majority = lambda x: True if x > 17 else False
however, this just simplifies to
majority = lambda x: x > 17
Conditional expressions are of the form
value_if_true if condition else value_if_false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论