Analysis of map,filter,reduce and internal realization principle in 3.python

Source: Internet
Author: User

A. Map function that applies a corresponding function to each element in any iteration sequence. (regardless of what type of sequence is processed, the last returned is a list.) )

The role has already been introduced in the title, so let's first say the use of the map function.

Map (the processing logic can be either a function or a lambda expression, an iterative sequence)

There is now a list.

L1 = [1,2,3,4,5]

Now you need to give each element in this list +1. (Of course, using a for loop can be done to process each element in the sequence, but using the map function is more convenient.) )

First, define a logical function of how to process each element in the sequence.

def plus (x):

return x + 1

# each element is +1

Print map (PLUS,L1)

# Apply the map function to each element of the L1.

The result of the output is.

>>>[2, 3, 4, 5, 6]


Of course, the logical functions in the incoming map function can also be represented using lambda expressions, as well as the example shown earlier.

L1 = [1,2,3,4,5]

Print map (lambda x:x+1,l1)

>>>[2, 3, 4, 5, 6]


So how does the map function do it? How is the internal function implemented? The following is an analysis of the inside of the map function.

The following function can implement the same functionality as the Python built-in map function.

def map_func (FUNC,SEQ):

Map_list = []

For i in SEQ:

Map_list.append (func (i))

Return map_list


Attention! The above-mentioned map function is used for the python2.7 version, for the version above Python3 does not apply, because starting from Python3, the map function no longer returns the list, the return is an iterator, if you want to become a list, you also need to use the list () function to do a conversion.



Two. Filter function to determine the selection of elements in a sequence. (The returned result is still a list.) )

When the Fileter function gets a sequence, it starts iterating through each element of the sequence, making an if judgment on each element, and adding the result to the list, false, and throwing it directly away.

The following is a simple use of the filter function.

A list of elements that are less than 5 in this list, all listed. (Of course ...) Such simple requirements can also be implemented with a for loop)

The filter function uses the same filter as the Map function (function, iterative sequence)

L1 = [1,2,3,4,5,6,7,8,9,10]


def greater_than (x):

If x < 5:

Return True

Else

Return False


Print filter (GREATER_THAN,L1)

>>>[1, 2, 3, 4]


The filter function can still use a lambda expression instead of a logical function.

Print filter (lambda x:x<5,l1)


Let's look at how the filter function works by manually writing a function that is identical to the filter function:

def filter_func (FUNC,SEQ):

Filter_list = []

For s in seq:

If Func (s):

Filter_list.append (s)

Return filter_list


Watch out! In this case, the filter function is the same as the map function, the usage is for python2.7, in the Python3 to make some changes to the function, the return is no longer a list, but an iterator.


Three. The reduce function, which merges all the elements in the sequence.

Now that you have a list, you need to multiply each element one at a time.


L1 = [1,2,3,4,5,6,7,8,9,10]

The #reduce function specifies that a function that supports a two-dollar operation must be passed

#下面就是定义好的逻辑函数

def bin_func (x, y):

return x * y


Print reduce (PLUS,L1)

>>> 3628800

is how the Reduce function works:

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/8E/D9/wKioL1jNAN2A82opAAC63NnUCNs013.png "title=" Reduce.png "alt=" Wkiol1jnan2a82opaac63nnucns013.png "/>


Next, dissect the implementation code of the reduce function.

def reduce_func (Func,seq,init_num=none):

If init_num! = None:

RET = Init_num

Else

ret = Seq.pop (0)

For i in SEQ:

Ret=func (ret,i) #如果这条逻辑不好理解的话, you can think carefully 1*2*3*4 = (1*2)

return ret


Print Reduce_func (BIN_FUNC,L1)


>>>3628800


The reduce function uses a lambda expression.

Print Reduce_func (lambda x,y:x*y,l1)


Attention! In Python3, this reduce function is placed in the Functools module, which needs to be poured into functools to use this function.


Finally, summarize:

The map function is used to process each element in a sequence.

The filter function is used to filter each element in a sequence.

The reduce function is used to do the merge operation of the sequence.

Reduce is the process of simplifying: each iteration, the last iteration result (the element that was init the first time, such as the first element of the SEQ without init) executes a two-dollar func function with the next element. In the reduce function, init is optional and, if used, is used as the first element of the first iteration.



This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1907951

Analysis of map,filter,reduce and internal realization principle in 3.python

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.