46 Simple Python Exercises-Higher order functions and list comprehensions

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了46 Simple Python Exercises-Higher order functions and list comprehensions相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

46 Simple Python Exercises (前20道题)

46 Simple Python Exercises-Higher order functions and list comprehensions

markdown python,exercise,iterator

Python基础lpthw - Exercise 40 模块类和对象

Data Analysis with Python : Exercise- Titantic Survivor Analysis | packtpub.com

文件操作&函数 exercise