The reduce () function of Python

Source: Internet
Author: User

Reduce () The function f passed in must receive two parameters, and reduce () calls function f repeatedly on each element of the list and returns the final result value. (That is, the return value for the last time)

#reduce () function fromFunctoolsImportReducedeff (x, y):returnx+yPrint(Reduce (f, [1, 3, 5, 7, 9]))#first two elements are computed: F (1, 3), and the result is 4;#then the result and the 3rd element are calculated: F (4, 5), the result is 9;#then the result and the 4th element are calculated: F (9, 7), the result is three;#then the result and the 5th element are calculated: F (16, 9), the result is;#since there are no more elements, the calculation ends and the result 25 is returned. #similar to summationreduce (f, [1, 3, 5, 7, 9], 100)# the#because the start is (100+1) to start the calculation

The reduce () function of Python

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.