Python拉姆达
在Python中,匿名函数意味着函数没有名称。我们已经知道 def 关键字用于定义普通函数,而 lambda 关键字用于创建匿名函数。它具有以下语法:
句法:
lambda arguments : expression
- 这个函数可以有任意数量的参数,但只有一个表达式,它被计算并返回。
- 一种是在需要函数对象的地方免费使用 lambda 函数。
- 您需要了解 lambda 函数在语法上仅限于单个表达式。
- 除了函数中的其他类型的表达式之外,它在特定的编程领域有各种用途。
示例 #1:
Python3
# Python program to demonstrate
# lambda functions
string ='GeeksforGeeks'
# lambda returns a function object
print(lambda string : string)
Python3
# Python program to demonstrate
# lambda functions
x ="GeeksforGeeks"
# lambda gets pass to print
(lambda x : print(x))(x)
Python3
# Python program to illustrate cube of a number
# showing difference between def() and lambda().
def cube(y):
return y*y*y;
g = lambda x: x*x*x
print(g(7))
print(cube(5))
Python3
# Python program to demonstrate
# lambda functions
def power(n):
return lambda a : a ** n
# base = lambda a : a**2 get
# returned to base
base = power(2)
print("Now power is set to 2")
# when calling base it gets
# executed with already set with 2
print("8 powerof 2 = ", base(8))
# base = lambda a : a**5 get
# returned to base
base = power(5)
print("Now power is set to 5")
# when calling base it gets executed
# with already set with newly 2
print("8 powerof 5 = ", base(8))
Python3
# Python program to demonstrate
# lambda functions inside map()
# and filter()
a = [100, 2, 8, 60, 5, 4, 3, 31, 10, 11]
# in filter either we use assignment or
# conditional operator, the pass actual
# parameter will get return
filtered = filter (lambda x: x % 2 == 0, a)
print(list(filtered))
# in map either we use assignment or
# conditional operator, the result of
# the value will get returned
mapped = map (lambda x: x % 2 == 0, a)
print(list(mapped))
输出:
at 0x7f268eb16f28>
在上面的示例中,打印函数没有调用 lambda,而是简单地返回函数对象和存储它的内存位置。
所以,为了让 print 首先打印字符串,我们需要调用 lambda 以便字符串通过 print。
示例 #2:
Python3
# Python program to demonstrate
# lambda functions
x ="GeeksforGeeks"
# lambda gets pass to print
(lambda x : print(x))(x)
输出:
GeeksforGeeks
示例 #3: lambda 和普通函数调用之间的区别
Python3
# Python program to illustrate cube of a number
# showing difference between def() and lambda().
def cube(y):
return y*y*y;
g = lambda x: x*x*x
print(g(7))
print(cube(5))
输出:
343
125
示例 #4: lambda函数在函数内部使用时会变得更有帮助。
Python3
# Python program to demonstrate
# lambda functions
def power(n):
return lambda a : a ** n
# base = lambda a : a**2 get
# returned to base
base = power(2)
print("Now power is set to 2")
# when calling base it gets
# executed with already set with 2
print("8 powerof 2 = ", base(8))
# base = lambda a : a**5 get
# returned to base
base = power(5)
print("Now power is set to 5")
# when calling base it gets executed
# with already set with newly 2
print("8 powerof 5 = ", base(8))
输出:
Now power is set to 2
8 powerof 2 = 64
Now power is set to 5
8 powerof 5 = 32768
我们还可以使用 map() 方法将列表推导式替换为 Lambda,不仅速度快而且效率高,我们还看看如何在 filter() 中使用 lambda。
示例 #5: filter() 和 map(
Python3
# Python program to demonstrate
# lambda functions inside map()
# and filter()
a = [100, 2, 8, 60, 5, 4, 3, 31, 10, 11]
# in filter either we use assignment or
# conditional operator, the pass actual
# parameter will get return
filtered = filter (lambda x: x % 2 == 0, a)
print(list(filtered))
# in map either we use assignment or
# conditional operator, the result of
# the value will get returned
mapped = map (lambda x: x % 2 == 0, a)
print(list(mapped))
输出:
[100, 2, 8, 60, 4, 10]
[True, True, True, True, False, True, False, False, True, False]