码迷,mamicode.com
首页 > 编程语言 > 详细

46 Simple Python Exercises-Higher order functions and list comprehensions

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

标签:call   lis   tools   directly   define   print   color   one   span   

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

标签:call   lis   tools   directly   define   print   color   one   span   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!