高阶函数02——Python提供的sum()函数可以接受一个list并求和, 请编写一个prod()函数,可以接受一个list并利用reduce()求积:

Python提供的sum()函数可以接受一个list并求和,
请编写一个prod()函数,可以接受一个list并利用reduce()求积:
from functools import reduce


#def prod(L):
#    def s(x,y):
#        return x * y
#    return reduce(s, L)

def prod(L):
    return reduce(lambda x, y: x*y,L)


print('3 * 5 * 7 * 9 =', prod([3,5,7,9]))
if prod([3,5,7,9]) == 945:
    print('successful')
else:
    print('error')
发布了41 篇原创文章 · 获赞 29 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43930694/article/details/104515600