How to use the built-in function map () and reduce () in Python3

Source: Internet
Author: User

Use of Map ()

Map () is used in the form of a map (f (x), Itera). Yes, it has two parameters, the first argument is a function, and the second is an iterative object. If you don't understand what a function is, don't know what an iterative object is, remember the following example:

1 def g (x): 2     return 3 * x34 >>> l=map (g,[1,3,4,5,7,10])5 > >> list (l)6 [3, 9, 12, 15, 21, 30]

The defined g (x) is the first parameter of map (), which is three times times the function of the parameter x.

The [1,3,4,5,7,10] list is the second parameter of G (x), which is a list. Yes, the list is an iterative object, of course, the iterative object is not only a list, but others can learn slowly. After calling the map function, g (x) will act on each element of the list, and we'll look at the effect after the effect:


[3, 9, 12, 15, 21, 30]

Yes, each element of L is three times times the original.
The function should understand.
An iterative object should be understood.
The point is, the map () function will work.
Congratulations!

Time is limited, and the following article will explain the reduce function in the simplest way.

How to use the built-in function map () and reduce () in Python3

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.