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]))
时间: 2024-10-29 17:05:00

46 Simple Python Exercises-Higher order functions and list comprehensions的相关文章

46 Simple Python Exercises (前20道题)

46 Simple Python Exercises This is version 0.45 of a collection of simple Python exercises constructed (but in many cases only found and collected) by Torbj?rn Lager ([email protected]). Most of them involve characters, words and phrases, rather than

46 Simple Python Exercises 16-22题

会贴出原题和答案,答案不是最优的,也反映了我的学习过程,如果有时间会更新优化的代码. Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n. #Write a function filter_long_words() that takes a list of words # and a

Javascript—Higher Order Functions

Higher order functions are functions that manipulate other functions. For example, a function can take other functions as arguments and/or produce a function as its return value. Such fancy functional techniques are powerful constructs available to y

46 Simple Python Exercises-Very simple exercises

4.Write a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise. def if_vowel(a): a=a.lower() if a in('a','e','i','o','u'): return True else: return Falseprint(if_vowel('A')) 原文地址:https://www.c

[Redux] Understand Redux Higher Order Reducers

Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a new reducer. In that new reducer, you can customize the behaviour of the original one which helps reducing the reducer logic. In this lesson, we'll se

【Python笔记】如何理解python中的generator functions和yield表达式

本篇笔记记录自己对Python的generator functions和yield表达式的理解. 1. Generator Functions Python支持的generator functions语法允许我们定义一个行为与iterator类似的函数,它可以被用在需要循环调用的场合.与普通函数相比,generator functions只是在函数定义中多了1个yield表达式,除此之外,没有其它特别之处. 当generator函数被创建时,python解释器会自动为它实现iteration p

[React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component

In this lesson we'll create a Higher Order Component (HOC) that takes care of the key property that React looks for when using map to create elements from a list. This HOC will allow us to wrap any component and it will take care of placing the keypr

Simple Python Dictionary :)

摘自 http://github.com/panweizeng/home/blob/master/code/python/dict/dict.py 支持简单的Ch to En 和En to Ch我把它放在 /usr/bin/dict 1234567891011 $ dict 白痴 单词:白痴 音标: bái chī 释义:idiot idiocy 例句:他很聪明,但有时举止像是白痴. 翻译:He is intelligent, but sometimes he behaves like an i

Beginner : Simple Python test

#!/usr/local/bin/python# -*- coding: utf-8 -*-.# line of utf-8 is for multi-language# optional 4-spaces before line rule for Python. import syssys.path.append("/homes/sli/pexpect-2.3/")import pexpectimport timeimport reimport os print "Welc