Python single-line function lambda (Xiaomi) plus reduce, map, filter (Rifle) applications

Source: Internet
Author: User

What is a lambda?

lambda定义匿名函数,并不会带来程序运行效率的提高,只会使代码更简洁。为了减少单行函数的定义而存在的。
lambda的使用大量简化了代码,使代码简练清晰。但是值得注意的是,这会在一定程度上降低代码的可读性。如果不是非常熟悉Python的人也许会对此很难理解。
如果可以使用for...in...if来完成的,坚决不用lambda。
如果使用lambda,lambda内不要包含循环,如果有,宁愿定义函数来完成,使代码获得可重用性和更好的可读性。如果你对你就喜欢用lambda来做,那也无可厚非,但是有内置函数减弱了代码的可读性!

 好了介绍完成了lambda,那就让我们来实际用一下,实例如下:

Let's take a look at the combination of millet plus rifles.
1. Reduce + lambda
What is reduce?

Python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(列表,元组等)中的所有数据进行如下操作:传给reduce中的函数func() (必须是一个二元操作函数)先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果。

For a detailed explanation of the python reduce, follow-up details, below we introduce the reduce + lambda application case:

Python3 count the number of repetitions of a string:

 from Import  = [''=reduce (Lambda a,x:a+x.count ("Learning  "), sentences,0)print(word_count)

>2

2. Map + Lambda
What is map?

map函数应用于每一个可迭代的项,返回的是一个结果list。如果有其他的可迭代参数传进来,map函数则会把每一个参数都以相应的处理函数进行迭代处理。map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。

Format: Map (func, seq1[, seq2 ...])

Python函数式编程中的map()函数是将func作用于seq中的每一个元素,并用一个列表给出返回值。如果func为None,作用同zip()。

The detailed application code is as follows:

Note here: When upgrading to Python3, there is a change in the map function that is, if you do not precede the map with the LIST,LAMBDA function will not be executed at all.


In Python2, map will return the result directly:

And then Python3, the return is a map object:

<map object at 0x7f381112ad50>

If you want to get the result, you must use list to act on the map object.

3. Filter + Map
What is the filter?

filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列,把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤。最终一次性返回过滤后的结果。和map()类似,filter()也接收一个函数和一个序列。和map()不同的时,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素。


The simple application is as follows:

Here is a brief introduction of Python built-in function millet plus rifles, more detailed introduction farewell, finish the call!

Python single-line function lambda (Xiaomi) plus reduce, map, filter (Rifle) applications

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.