本文共 3554 字,大约阅读时间需要 11 分钟。
Lambda Expressions are ideally used when we need to do something simple and are more interested in getting the job done quickly rather than formally naming the function. Lambda expressions are also known as anonymous functions.
当我们需要做一些简单的事情并且对快速完成工作而不是正式命名函数更感兴趣时,Lambda表达式是理想的选择。 Lambda表达式也称为匿名函数。
Lambda Expressions in Python are a short way to declare small and anonymous functions (it is not necessary to provide a name for lambda functions).
Python中的Lambda表达式是声明小型匿名函数的一种简短方法(不必为lambda函数提供名称)。
Lambda functions behave just like regular functions declared with the def
keyword. They come in handy when you want to define a small function in a concise way. They can contain only one expression, so they are not best suited for functions with control-flow statements.
Lambda函数的行为就像使用def
关键字声明的常规函数一样。 当您想以简洁的方式定义小功能时,它们会派上用场。 它们只能包含一个表达式,因此它们最不适合带有控制流语句的函数。
lambda arguments: expression
lambda arguments: expression
Lambda functions can have any number of arguments but only one expression.
Lambda函数可以具有任意数量的参数,但只能有一个表达式。
# Lambda function to calculate square of a numbersquare = lambda x: x ** 2print(square(3)) # Output: 9# Traditional function to calculate square of a numberdef square1(num): return num ** 2print(square(5)) # Output: 25
In the above lambda example, lambda x: x ** 2
yields an anonymous function object which can be associated with any name. So, we associated the function object with square
. So from now on we can call the square
object like any traditional function, for example square(10)
在上面的lambda示例中, lambda x: x ** 2
产生一个匿名函数对象,该对象可以与任何名称关联。 因此,我们将功能对象与square
相关联。 因此,从现在开始,我们可以像任何传统函数一样调用square
对象,例如square(10)
lambda_func = lambda x: x**2 # Function that takes an integer and returns its squarelambda_func(3) # Returns 9
lambda_func = lambda x: True if x**2 >= 10 else Falselambda_func(3) # Returns Falselambda_func(4) # Returns True
my_dict = {"A": 1, "B": 2, "C": 3}sorted(my_dict, key=lambda x: my_dict[x]%3) # Returns ['C', 'A', 'B']
Let’s say you want to filter out odd numbers from a list
. You could use a for
loop:
假设您要从list
过滤掉奇数。 您可以使用for
循环:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]filtered = []for num in my_list: if num % 2 != 0: filtered.append(num)print(filtered) # Python 2: print filtered# [1, 3, 5, 7, 9]
Or you could write this as a one liner with list-comprehensions:
或者,您可以将其编写为具有列表理解力的一体式班轮:
filtered = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if x % 2 != 0]
But you might be tempted to use the built-in filter
function. Why? The first example is a bit too verbose and the one-liner can be harder to understand. But filter
offers the best of both words. What is more, the built-in functions are usually faster.
但是您可能会想使用内置的filter
功能。 为什么? 第一个示例过于冗长,难以理解。 但是filter
提供了两个词中最好的。 而且,内置功能通常更快。
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]filtered = filter(lambda x: x % 2 != 0, my_list)list(filtered)# [1, 3, 5, 7, 9]
NOTE: in Python 3 built in functions return generator objects, so you have to call list
. In Python 2, on the other hand, they return a list
, tuple
or string
.
注意:在Python 3中,内置函数返回生成器对象,因此您必须调用list
。 另一方面,在Python 2中,它们返回一个list
, tuple
或string
。
So what happened? You told filter
to take each element in my_list
and apply the lambda expressions. The values that return False
are filtered out.
所以发生了什么事? 您告诉filter
接受my_list
每个元素并应用lambda表达式。 返回False
的值将被过滤掉。
翻译自:
转载地址:http://qduzd.baihongyu.com/