python map

map() 会根据提供的函数对指定序列做映射。

例子:

  列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数,最终输出[16,25]

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


# 自定义函数
def square(x):
    return x ** 2


res = map(square, mylist)

# print(list(res))

filter_res = [x for x in res if x > 10]

print(filter_res)

# 使用 lamada 表达式
res1 = map(lambda x: x * x, mylist)
print(list(res1))
原文地址:https://www.cnblogs.com/newlangwen/p/12592402.html