数据科学中的python lambda表达式

Coding efficiently is one of the key premises to the use case of Python and Lambda expressions are no different. Python lambda’s are anonymous functions which involve small and concise syntax, whereas at times, regular functions can be too descriptive and quite long.

高效编码是Python用例的关键前提之一,Lambda表达式也不例外。 Python lambda是anonymous函数,涉及小的简洁语法,而有时,常规函数可能描述性太强且冗长。

Python is one of a few languages which had lambda functions added to their syntax whereas other languages, like Haskell, uses lambda expressions as a core concept.

Python是在其语法中添加了lambda函数的几种语言之一,而其他语言(例如Haskell)使用lambda表达式作为核心概念。

Whatever your use-case of a Lambda function, it’s really good to know what they’re about and how to use them.

无论您使用Lambda函数的用例如何,了解它们的含义以及如何使用它们都是非常好的。

为什么要使用Lambda函数? (Why Use Lambda Functions?)

The true power of a lambda function can be shown when used inside another function but let’s start on the easy step.

当在另一个函数中使用时,可以显示lambda函数的真正功能,但让我们从简单的步骤开始。

Say you have a function definition that takes one argument, and that argument will be added to an unknown number:

假设您有一个接受一个参数的函数定义,该参数将被添加到一个未知数中:

def identity(x):
... return x + 10

However this can be compressed into a simple one-liner as follows:

但是,可以将其压缩为简单的单线,如下所示:

identity = lambda a : a + 10

This function can then be used as follows:

然后可以按以下方式使用此功能:

identity(10) 

which will give the answer 20.

这将给出答案20

Now with this simple concept, we can also extend this to have more than one input as follows:

现在有了这个简单的概念,我们还可以将其扩展为具有多个输入,如下所示:

myfunc = lambda a, b, c : a + b + c

So the following:

因此,以下内容:

myfunc(2,3,4)

Will give the function 9. It’s really that simple!

将赋予功能9 。 真的就是这么简单!

Now a really cool use case of Lambda expressions occurs when you use lambda functions within functions. Take the following example:

现在,当您在函数中使用lambda函数时,就会出现一个非常酷的Lambda表达式用例。 请看以下示例:

def myfunc(n):
return lambda a : a * n

Here, the function myfunc returns a lambda function which multiplies the input a by a pre-defined integer, n. This allows the user to create functions on the fly:

在这里,函数myfunc返回一个lambda函数,该函数将输入a乘以预定义的整数n 。 这允许用户即时创建函数:

mydoubler = myfunc(2)
mytripler = myfunc(3)

As can be seen, the function mydoubler is a function that simply defines an input by the number 2, whereas mytripler multiplies an input by 3. Test it out!

可以看出,函数mydoubler是一个简单地用数字2定义输入的函数,而mytripler将输入乘以3。测试一下!

print(mydoubler(11))
print(mytripler(11))

This brings about the answers 22 and 33.

这带来了答案2233

Image for post
Photo by Ian Stauffer on Unsplash
Ian StaufferUnsplash上的 照片

Lambdas是Pythonic还是不是? (Are Lambdas Pythonic or Not?)

According to the style-guide of Python (PEP 8), it describes the following which actually recommends users TO NOT use Lambda expressions:

根据Python的样式指南( PEP 8 ),它描述了以下内容,实际上建议用户不要使用Lambda表达式

Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier.

始终使用def语句而不是将lambda表达式直接绑定到标识符的赋值语句。

Yes:

是:

def f(x): 
return 2*x

No:

没有:

f = lambda x: 2*x

f = lambda x: 2*x

The logic around this is probably more to do with readability than any personal vendetta against lambda expressions. Agreeably, they can make it a bit more difficult to understand the use case but as a coder who prefers efficiency and simplicity in code, I do feel that there’s a place for them.

围绕此问题的逻辑可能与可读性有关,而不是针对lambda表达式的任何个人仇恨。 可以接受的是,它们会使理解用例变得更加困难,但是作为一个偏爱代码效率和简洁性的编码人员,我确实觉得他们有地方。

However, readable code has to be the most important feature of any code — debatably more important than efficiently run code.

但是,可读代码必须是所有代码中最重要的功能—相比有效运行的代码,其重要性要高得多。

示例数学公式 (Example Math Formulas)

Mean:

均值

mu = lambda x: sum(x) / len(x)

Variance:

方差

variance = lambda x: sum((x - mu(x))**2) / (len(x) - 1)

Thanks for reading! If you have any messages, please let me know!

谢谢阅读! 如果您有任何留言,请告诉我!

Keep up to date with my latest articles here!

在这里了解我的最新文章!

翻译自: https://medium.com/code-python/python-lambda-expressions-in-data-science-a25625e3038d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值