首页 > 编程语言 > 详细

46 Simple Python Exercises-Higher order functions and list comprehensions

时间:2017-05-28 14:46:28      阅读:287      评论:0      收藏:0      [点我收藏+]

26. Using the higher order function reduce(), write a function max_in_list() that takes a list of numbers and returns the largest one. Then ask yourself: why define and call a new function, when I can just as well call the reduce() function directly?

from functools import reduce

def max_in_list(num_list):
    def max_of_two(a, b):
        return a if a >= b else b
    biggest = float("-inf")
    return reduce(max_of_two, num_list, biggest)

print(max_in_list([100,-2,3,4,5]))

 

46 Simple Python Exercises-Higher order functions and list comprehensions

原文:http://www.cnblogs.com/junejs/p/6915679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!