map函数

map会将一个函数映射到一个输入列表的所有元素上,如:

items = [1,2,3,4,5]

squared = []

for i in items:

    squared.append(i**2)

return squared

map可以让我们用一种简单而漂亮的方式实现:

items = [1,2,3,4,5]

squared = list(map(lambda x : x**2, items))

map不仅可以用于一列表的简单元素输入,还可以用于一列表的函数输入:

def add(x):

    return (x+x)

def multiply(x):

    return (x*x)

funcs = [add,multiply]

for i in range(5):

    value = list(map(lambda x : x (i), funcs))

    print(value)

你可能感兴趣的:(map函数)