Map and reduce for Python

Source: Internet
Author: User

Map (f, Itera) # Use F (x) for each element

lambda x:x**2>>> l = map (sq,[-1,0,1,2,-3])>>> list (l) [1, 0, 1, 4, 9]

Of course, you can also pass in two parameters:

Lambda x, y:x + y>>> l = map (lambda x, y:x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, ten  ])>>> list (l) [3, 7, 11, 15, 19]

Reduce (f (x), Itera) # x1 The result F (x1) Continuation and the next element of the sequence x2 do the cumulative calculation for the previous

function F must have two parameters x, y

 from Import Reduce def # define an Add function        return x+y>>> reduce (add,[1,2,3,4,6])16

Add (x, y) is a function that we define, which passes the Add function and the [1,2,3,4,6] list to the reduce function, which is equivalent to 1+2+3+4+6 = 16. That is, the result continues and the next element of the sequence is accumulated.

Map and reduce for 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.