每天30分钟学python_每天学习Python 10分钟21

每天30分钟学python

每天10分钟的Python速成课程 (10 minutes a day Python crash course)

This is a series of short 10 minute Python articles helping you to boost your knowledge of Python. I try to post an article each day (no promises), starting from the very basics, going up to more complex idioms. Feel free to contact me on LinkedIn for questions or requests on particular subjects of Python you want to know about.

这是一系列简短的10分钟Python文章,可帮助您增强Python知识。 我尝试每天发布一篇文章(不做任何保证),从最基础的内容开始,再到更复杂的习惯用法。 如果您想了解有关Python特定主题的问题或要求,请随时通过LinkedIn与我联系。

For new-comers, the lambda function can be somewhat intimidating. I have also avoided the use of these functions for some time as I never understood true use of them. Nowadays, I do see a benefit of the construct but it is rather small. You will come across these functions and therefore, it is good to know what is happening. Lambda functions are also called nameless functions or anonymous functions which describes them much better. A lambda function is an inline definition of a function which does not have a name. Apart from not having a name, what is the difference between a regular function and the lambda function? Practically nothing! Then the next question is: why to use a lambda function when a regular function does the same thing? Indeed, most of the time, a regular function is the way to go. However, sometimes you only need to do an operation once. While you could still use a regular function, an inline function (the lambda function) could be somewhat easier.

对于新手来说,lambda函数可能有些吓人。 一段时间以来,我也避免使用这些功能,因为我从未真正使用过它们。 如今,我确实看到了该构造的好处,但是它很小。 您将遇到这些功能,因此,很高兴知道发生了什么。 Lambda函数也称为无名函数或匿名函数,它们可以更好地描述它们。 Lambda函数是没有名称的函数的内联定义。 除了没有名称之外,常规函数和lambda函数之间有什么区别? 几乎没有! 接下来的问题是:当常规函数执行相同的操作时,为什么要使用lambda函数? 确实,在大多数情况下,常规功能是必经之路。 但是,有时您只需要执行一次操作。 尽管您仍然可以使用常规函数,但是内联函数(lambda函数) 可能会更容易一些。

The definition of a lambda function is short: it starts with the lambda keyword followed by one or more parameters separated by commas. The steps of the function itself follow after a semicolon. Let’s have a look at an example:

lambda函数的定义很简短:它以lambda关键字开头,后跟一个或多个用逗号分隔的参数。 函数本身的步骤在分号之后。 让我们看一个例子:

In this example we have a list of expenses. Each expense is a tuple with a name and the costs. While there are other ways (like list comprehensions), we choose to use a map() to select all the costs. Map() expects a function and an iterable. We could now create a regular function that selects the second item of the tuple but we only use it once. This means that a lambda function would be perfect here. The map() executes our function on each tuple and returns a list of all the costs, followed by a sum. Now we have a look at some more examples (disclaimer: not all are useful):

在此示例中,我们列出了费用清单。 每个费用都是一个带有名称和费用的元组。 虽然还有其他方法(例如列表推导),但我们选择使用map()来选择所有成本。 Map()需要一个函数和一个可迭代的函数。 现在,我们可以创建一个常规函数来选择元组的第二项,但只使用一次。 这意味着lambda函数在这里将是完美的。 map()在每个元组上执行我们的函数,并返回所有成本的列表,然后是总和。 现在我们来看一些其他示例(免责声明:并非所有示例都有用):

In the example we show that a lambda expression returns a reference to the nameless function. The function is now catched by a variable, it will not be ‘garbage-collected’. This eliminates the benefit of the lambda function, namely a one-time nameless function, which will be disposed after its use. If you add a reference to a lambda function, just write a regular function instead. Regular functions are much more readable. Nevertheless, you could catch the lambda function into a reference, and the variable then acts as a regular function (just don’t do it (-;).

在示例中,我们显示了lambda表达式返回对无名函数的引用。 该函数现在被变量捕获,不会被“垃圾回收”。 这消除了lambda函数(即一次性无名函数)的好处,该函数将在使用后被废弃。 如果添加对lambda函数的引用,则只需编写常规函数即可。 常规函数更具可读性。 但是,您可以将lambda函数捕获到引用中,然后该变量将充当常规函数(只是不要这样做(-;)。

Image for post
Meme generated on imgflp.com
模因在imgflp.com上生成

Just for fun, you could also directly call the reference without catching it in a variable. While it does not make much sense it is possible and completely valid. The additional parenthesis are required or a function identifier is returned.

只是为了好玩,您还可以直接调用引用,而不必将其捕获到变量中。 虽然没有太大意义,但它是可能的并且完全有效。 需要附加括号或返回函数标识符。

To prove that regular functions and lambda functions are identical we can inspect the byte-code from the Python interpreter. For this, we can make use of the dis module, which visualizes the actual byte-code executed. All Python commands can be split down to a small number of byte-code operations. Here we compare the byte-code between the regular and the lambda and see that we have an exact match. They are identical in byte-code!

为了证明常规函数和lambda函数是相同的,我们可以检查Python解释器中的字节码。 为此,我们可以使用dis模块,该模块可视化执行的实际字节码。 可以将所有Python命令分解为少量的字节码操作。 在这里,我们比较常规和lambda之间的字节码,看看我们有一个完全匹配。 它们的字节码相同!

The previous example shows two new functions which can be useful: filter() and reduce(). As the name suggests, filter() is used to filter an iterable. Each item of the iterable is put into a function that must return a boolean. If the boolean is true, the value is kept. If the boolean is false, the value is omitted. We can supply a regular function, but if it is a one-timer, a lambda functions might be easier.

前面的示例显示了两个有用的新函数:filter()和reduce()。 顾名思义,filter()用于过滤可迭代对象。 每个iterable项都放入一个必须返回布尔值的函数中。 如果布尔值为true,则保留该值。 如果布尔值为false,则省略该值。 我们可以提供常规函数,但如果是一次性函数,则lambda函数可能会更容易。

Reduce() is a method that is common in many other languages. It takes an iterable and uses the supplied function to reduce the iterable to a single value (of the same type). What is happening is that it takes the first two values of the iterable and creates a new value according to the function supplied. It repeats this operation until only a single value is left. Again, this could be a great spot for a lambda function.

Reduce()是许多其他语言中常见的方法。 它需要一个Iterable,并使用提供的函数将Iterable减少为单个值(相同类型)。 发生的情况是,它采用了iterable的前两个值,并根据提供的函数创建了一个新值。 重复此操作,直到仅剩一个值为止。 同样,这可能是lambda函数的一个好地方。

今天的练习: (Practice for today:)

While a life without lambda functions is quite possible, I think it is a good nice to know. You will encounter them so now and then. In this assignment we will use reduce() to find the maximum value of a set of values.

虽然没有lambda函数的生活很有可能,但我认为这是一个很好的认识 。 您会不时遇到它们。 在此分配中,我们将使用reduce()查找一组值的最大值。

Assignment:Use reduce to find the maximum of the 1000 values.

分配:使用reduce查找1000个值中的最大值。

Hints:1. the lambda function has two parameters2. use the inline if to return the correct value

提示:1。 lambda函数具有两个参数2。 使用内联是否返回正确的值

A solution is posted on my Github.

一个解决方案发布在我的Github上。

If you have any questions, feel free to contact me through LinkedIn.

如有任何疑问,请随时通过LinkedIn与我联系。

翻译自: https://towardsdatascience.com/learning-python-10-minutes-a-day-21-f1d8eec408d

每天30分钟学python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值