“lambda”到底是什么?在Python中?
我想知道Python中的lambda
到底是什么?以及使用地点和原因。 谢谢
I want to know what exactly is lambda
in python? and where and why it is used.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
Lambda 更多的是一种概念或编程技术,而不是其他任何东西。
基本上,它的想法是,你得到一个作为另一个函数而不是对象或原始类型的结果返回的函数(Python 中的第一类对象)。我知道,这很令人困惑。
请参阅 python 文档 中的示例:
因此 make_incrementor 创建了一个使用n 在其结果中。您可以有一个将参数增加 2 的函数,如下所示:
这在函数式编程和 lisp 等函数式编程语言中是一个非常强大的想法。方案。
希望这有帮助。
Lambda is more of a concept or programming technique then anything else.
Basically it's the idea that you get a function (a first-class object in python) returned as a result of another function instead of an object or primitive type. I know, it's confusing.
See this example from the python documentation:
So make_incrementor creates a function that uses n in it's results. You could have a function that would increment a parameter by 2 like so:
This is a very powerful idea in functional programming and functional programming languages like lisp & scheme.
Hope this helps.
Lambda 不是匿名函数。 Lambda 是匿名表达式。
它们的访问方式类似于函数,但它们不是同一件事。函数允许复杂的任务:流程控制、变量声明和包含表达式的语句列表。表达式只是函数的一部分,这就是 lambda 所提供的功能。与功能相比,它们受到严重限制。
Python 不支持匿名函数。有关这样做的语言示例,请参阅 Javascript 和 Lua。
(注意:在函数语言中调用 lambda 匿名函数是正确的,其中使用了“函数”的数学定义,但在过程语言中该词的含义与数学中的含义截然不同。)
Lambdas are not anonymous functions. Lambdas are anonymous expressions.
They're accessed like functions, but they're not the same thing. Functions allow complex tasks: flow control, variable declarations and lists of statements containing expressions. Expressions are merely one part of a function, and that's what lambdas give you. They're severely limited compared to functions.
Python does not support anonymous functions. For examples of languages that do, see Javascript and Lua.
(Note: It's correct to call lambdas anonymous functions in functional languages, where the mathematical definition of "function" is used, but in procedural languages the word has a very different meaning than in mathematics.)
lambda
允许您定义与代码内联的简单的未命名函数,例如作为参数。如果您计划仅使用该函数一次,因此不希望命名函数使您的代码变得混乱,那么这非常有用。假设您有一个数字列表 (
thelist
),并且您希望获得一个相同长度的列表,其中每个数字加倍。您可以执行以下操作,而不是定义times_two
函数:map(lambda x: x * 2, thelist)
lambda
还可以使 < a href="http://en.wikipedia.org/wiki/Currying" rel="nofollow">柯里化 更方便。lambda
allows you define simple, unnamed functions inline with your code, e.g. as an argument. This is useful if you plan to use the function only once, and therefore don't want to clutter your code with a named function.Let's say you have a list of numbers (
thelist
), and you want to get a list of the same length, with each number doubled. Rather than defining atimes_two
function, you could do something like this:map(lambda x: x * 2, thelist)
lambda
also makes Currying more convenient.Lambda 函数不是 Python 特有的概念,而是匿名函数(即没有名称的函数)的通用编程术语。在 Python 中,它们通常用于需要将一个简单函数作为参数传递给另一个函数的情况。
列表上的
sort
方法采用参数key
,该参数是一个用于计算列表排序值的函数。想象一下,您正在对两个元素元组的列表进行排序,并且您希望根据第一个元素对列表进行排序。您需要将一个函数传递给返回第一个元素的key
。你可以这样做:或者,更简洁地,你可以这样做:
Lambda functions are not a Python specific concept, but are a general programming term for anonymous function, i.e. functions without a name. In Python they are commonly used where you need to pass a simple function as a parameter to another function.
The
sort
method on lists takes a parameterkey
which is a function that is used to calculate the value the list is sorted on. Imagine you are sorting a list of two element tuples, and you want to sort the list based on the first element. You need to pass a function tokey
which returns the first element. You could do this:or, much more concisely you can do:
lambda 构造是定义计算单个表达式的简单函数的更短方法。
def
语句可能会很不方便,并且会使代码变得更长、更分散并且更难以阅读。两者创建的函数在工作方式上几乎相同,不同之处在于 lambda 仅限于返回其值的单个表达式,而 def为函数分配一个名称,并将其添加到同名的局部变量中。另外,lambda
可以直接在表达式中使用,而def
是一个语句。会给你几乎相同的结果,
并且你可以直接在表达式中使用它
,而使用
def
会不太简洁The
lambda
construct is a shorter way to define a simple function that calculates a single expression. Thedef
statement can be inconvenient and make the code longer, broken up and harder to read through. The functions created by the two are virtually the same by the way they work, the difference is thatlambda
is limited to a single expression the value of which is returned and thatdef
assigns a name to the function and adds it to the local variables by that same name. Alsolambda
can be used in an expression directly, whiledef
is a statement.Would give you almost the same result as
And you can use it directly in an expression
which with
def
would be less conciselambda 是一种定义匿名内联函数来完成某些任务的方法,而不是使用 def 关键字定义并调用它。将其视为不需要重用函数时可以使用的简写。在任何使用 lambda 的地方,都可以替换显式调用的函数:它的使用完全是可选的,并作为编码风格的问题留给程序员。
示例代码(不带 lambda):
示例代码(带 lambda):
对
lambda
的一个很好的初学者讨论:DiveIntPython.org - Lambda
lambda
is a way of defining an anonymous in-line function to accomplish some task versus defining it with thedef
keyword and calling it. Think of it as a shorthand you can use when you won't need to reuse a function. Anywhere you use lambda, you could substitute an explicitly called function: it's use is completely optional and left to the programmer as a matter of coding style.Example code (without lambda):
Example code (with lambda):
A good beginner's discussion of
lambda
:DiveIntPython.org - Lambda
这是一个内联匿名函数。
It's an inline anonymous function.
lambda
是一个匿名函数,通常用于需要快速计算的东西。示例(这在 LexYacc 命令解析器中使用):
assign_command = lambdadictionary, command: lambda command_function:dictionary.setdefault(command, command_function)
这是一个装饰器。将我们从 LexYacc 解析器获得的命令放在前面:
@assign_command(_command_handlers, 'COMMAND')
这本质上是根据定义的 LexYacc 语言构建一个 Python 脚本。
或者,更简单一点:
`x = lambda x: return x > 0 和 x & (x-1) == 0
lambda
is an anonymous function, usually used for something quick that needs computing.Example (This is used in a LexYacc command parser):
assign_command = lambda dictionary, command: lambda command_function: dictionary.setdefault(command, command_function)
This is a decorator. Put before a command we'd have from the LexYacc parser:
@assign_command(_command_handlers, 'COMMAND')
This essentially builds a python script from the LexYacc language defined.
Or, a bit simpler:
`x = lambda x: return x > 0 and x & (x-1) == 0
Lambda 可以直观地解释为代数函数。
假设一个基本代数 y = x**2 + 1
如果直接键入,
则会发生错误并报告应首先定义变量“x”。
然而,“x”是未知的,如何预先定义它。
Lambda 有帮助:
这是 lambda 的核心,
它是用编程语言编写内联函数。
Lambda can be interpreted intuitively as algebra function.
Suppose a basic algebra y = x**2 + 1
if typed directly,
error occurs and reports that variable 'x' should be defined firstly.
nevertheless,'x' is unknown, how can it be pre-defined.
Lambda helps:
That's core about lambda,
It's in programming language to write an inline function.
Lambda 是 Python 编程语言中的一个匿名函数,前面不再带有 def 语句,而是简单地称为 lambda。
# returns 4
与 lambda 函数完全相同。
# 返回 4
有关更多详细信息,您可以查看此博客文章。
http://friendpython.herokuapp.com/2017/1/anonymous-function -lambda/ 或此处
Lambda is an anonymous function in Python programming language, instead of bearing a def statement in the front it is simply called and written lambda.
# returns 4
Exactly same you can do with a lambda function.
# returns 4
For more details you can look up in this blog post.
http://friendlypython.herokuapp.com/2017/1/anonymous-function-lambda/ or here
我想从使用角度用一些外行人的方式来解释这一点,因为所有的技术点都已经涵盖了。
lambda 函数就像任何其他函数一样,唯一的不同之处(除了语法和它不能重用这一事实之外)是,当我们想要快速编写一个带有参数的函数时使用它并且仅使用一个表达式返回一个值。
例如:
如果函数所做的只是接受一个参数并加 1,那么使用 lambda 函数是比普通函数更好的方法。
但如果你的函数在返回值之前需要多行处理,那么我们需要使用普通的函数定义,然后调用它。
I would like to explain this with a little layman approach in terms of the usage perspective, since all the technical points are already covered.
A lambda function is just like any other function, only thing that makes it different (other than the syntax and the fact that it can't be reused), is that it is used when we want to quickly write a function which takes an argument and just returns a value using only one expression.
For instance:
If all that a function does is take a argument and add 1 to it then using a lambda function is a better approach than a normal function.
But if your function requires more than one line of processing before returning the value, then we need to use a normal function definition and then call it.