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 ']