Python Advanced Map () function

Source: Internet
Author: User

The main function of the map () function is to reduce the code significantly when you perform the same operation on a batch element.

Detailed map function Parameters:

Map (param1,param2,***)

param1: function name, functions to be processed for bulk elements

PARAM2~PARAMX: An Iterative object, (list, meta-ancestor, iterator ...) In other words: can execute for the x in Param: The statement can be all)

When map has only two parameters:

For example:

1 def function (x): 2 ...     return x*x34 >>> list1 = [x-ray]5 > >> Map (function,list1)6 [1, 4, 9]7

When the map parameter is multiple:

Note: The number of map parameters and the number of function parameters of the relationship: the number of map parameters is x, the number of function is x-1, otherwise it will be an error

For example (in case of error):

1>>>deffunction (x):2...returnx*x3 ... 4>>> List1 = [a]5>>> List2 = [4,5,6]6>>> LIST3 = [7,8,9]7>>>map (FUNCTION,LIST1,LIST2,LIST3)8 Traceback (most recent):9File"<stdin>", Line 1,inch<module>TenTypeerror:function () takes exactly 1 argument (3 given)

The correct usage should be:

1 def function (x, y, z): 2 ...     return x+y+z34 >>> list1 = []5 > >> list2 = [4,5,6]6 >>> list3 = [7,8,9]7 >>>  Map (FUNCTION,LIST1,LIST2,LIST3)8 [12, 15, 18]

Python Advanced Map () function

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.