MST

星途 面试题库

面试题:Python高阶函数与lambda表达式的应用

请使用Python的高阶函数(如map、filter、reduce)和lambda表达式,实现对列表[1, 2, 3, 4, 5]中每个元素进行平方操作,然后过滤出偶数结果,并对这些偶数结果求和。请给出完整代码。
22.3万 热度难度
编程语言Python

知识考点

AI 面试

面试题答案

一键面试
from functools import reduce

nums = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, filter(lambda x: x % 2 == 0, map(lambda x: x ** 2, nums)))
print(result)