Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join 11 million other learners and get started learning Python for data science today!
Good news! You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount
Lambda 函数
正常情况下,我们使用 def
关键字在代码的某处定义一个函数,并在需要使用时调用它。
def sum(a,b):
return a + b
a = 1
b = 2
c = sum(a,b)
print(c)
现在,我们可以使用 Python 的 lambda 函数来代替在某处定义函数并调用它。lambda 函数是在我们使用它的地方定义的内联函数。因此,对于一次性的使用,我们无需在某处声明一个函数并再次查看代码。
它们不需要名称,因此也被称为匿名函数。我们使用关键字 lambda
来定义一个 lambda 函数。
your_function_name = lambda inputs : output
因此,上述使用 lambda 函数的 sum 示例将是:
a = 1
b = 2
sum = lambda x,y : x + y
c = sum(a,b)
print(c)
在这里,我们将 lambda 函数赋值给变量 sum,并在给定参数 a 和 b 时,它像普通函数一样工作。
练习
编写一个使用 lambda 函数的程序,用于检查给定列表中的数字是否为奇数。对于每个元素,如果数字是奇数则打印 "True",否则打印 "False"。
l = [2,4,7,3,14,19]
for i in l:
# your code here
l = [2,4,7,3,14,19]
for i in l:
# your code here
my_lambda = lambda x : (x % 2) == 1
print(my_lambda(i))
test_output_contains("False")
test_output_contains("False")
test_output_contains("True")
test_output_contains("True")
test_output_contains("False")
test_output_contains("True")
success_msg("Nice work!")
This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!