A detailed explanation of map function in Python3

Source: Internet
Author: User
Reprint http://www.cnblogs.com/superxuezhazha/p/5714970.html
 
 
 
The map () function
map () is a higher-order function built into Python that receives a function f and a list, and then returns a new list by using the function f to act on each element of the list in turn. for

example, for list [1, 2, 3, 4, 5, 6, 7, 8, 9]

if you want to square each element of the list, you can use the map () function:



so we just need to pass in the function f (x) =x *x, you can use the map () function to complete this calculation:

def f (x): Return
    x*x
print map (f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
output results:

[1, 4 , 9, A,,,, and eight,
I note: the map () function does not change the original list, but instead returns a new list.

using the map () function, you can convert a list to another list, and only need to pass in the conversion function.

Since the list contains elements that can be of any type, the map () can handle only a list that contains only numeric values, and in fact it can handle lists that contain any type, as long as the function f passed in can handle this type of data.

Task
assumes that the user has entered an English name that is not canonical, does not follow the first letter capitalization, followed by the lowercase letter of the rule, please use the map () function, a list (containing a number of nonstandard English names) into a list containing the canonical English name:

input: [' Adam ', ' Lisa ', ' Bart ']
output: [' Adam ', ' Lisa ', ' Bart ']

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.