Python: map () function

description

map () will do the mapping function in accordance with the specified sequence provided.

The first parameter argument function to call the function function of each element in the sequence, each time the function returns a new list of function return values.
grammar

map () function syntax:

map(function, iterable, …)

parameter

function -- 函数
iterable -- 一个或多个序列

return value

Python 2.x return list.

Python 3.x returns an iterator.
Examples

The following example shows a map () method is used:

>>> def square (x): # calculate the number of squares

… return x ** 2

>>> map (square, [1,2,3,4,5]) # calculates the square of each element of the list

[1, 4, 9, 16, 25]

map (lambda x: x ** 2 , [1, 2, 3, 4, 5]) # lambda anonymous function using
[1, 4, 9, 16, 25]

Two lists, a list of data of the same position are summed

>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

[3, 7, 11, 15, 19]

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/93371251