Python Full Stack development "supplement" the difference between the map function and the reduce function

Source: Internet
Author: User

① in terms of parameters:
Map () Function:
Map () contains two parameters, the first one is a function, the second is a sequence (list or tuple). Where a function (that is, a function of the first parameter position of a map) can receive one or more parameters.
Reduce () function:
The first parameter of reduce () is a function, and the second is a sequence (list or tuple). However, its function must receive two parameters.

② from the numerical effect on the transfer in terms of:
Map () is the function of the passed in order to each element of the sequence, each element is alone by the function "action" once;
Reduce () is the function of the descendant to the first element of the sequence to get the result, the result will continue to work with the next element (cumulative calculation),
The end result is the interaction of all elements.
Practice:
Calculation: Lambda and reduce implement 1 to 100 cumulative from Functools import reduceprint (Reduce (lambda x,y:sum ([x, Y], range (1,101)))  # The first parameter is a function, the second parameter is a sequence print (Map (lambda x,y:sum ([x, Y], range (1,101)))  #第一个参数是一个函数, the second argument is a sequence print (list (map) ( Lambda X,y:sum ([x, Y]), Range (1,101), Range (1,101)))  #第一个参数是一个函数, the second parameter is a sequence

Printing results:





Python Full Stack development "supplement" the difference between the map function and the reduce function

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.