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]))
【june】reduce和map是函数式编程最明显的特点,也是两个相反的过程。这个函数在真实的编程中用的并不多,完全可以用for来实现同样的功能,只是代码多几行而已。
27. Write a program that maps a list of words into a list of integers representing the lengths of the correponding words. Write it in three different ways: 1) using a for-loop, 2) using the higher order function map()
, and 3) using list comprehensions.
# 1) use loop
def words_to_length(words):
lens = []
for word in words:
lens.append(len(word))
return lens # 2) use map
def words_to_length(words):
return list(map(len, words)) # 3) use list comprehension
def words_to_length(words):
return [len(word) for word in words] words_to_length(["i", "am", "newbie"])
28. Write a function find_longest_word()
that takes a list of words and returns the length of the longest one. Use only higher order functions.
from functools import reduce def find_longest_word(words):
return reduce(max, map(len, words)) print(find_longest_word(["a", "student", "good"]))
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 construct ...
- 46 Simple Python Exercises-Very simple exercises
46 Simple Python Exercises-Very simple exercises 4.Write a function that takes a character (i.e. a s ...
- [CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: Higher Order Functions
[CS61A] Lecture 5&6&7. Environments & Design & Functions Examples & Homework 2: ...
- [Redux] Understand Redux Higher Order Reducers
Higher Order Reducers are simple reducer factories, that take a reducer as an argument and return a ...
- [React] Higher Order Components (replaces Mixins)
Higher order components will allow you to apply behaviors to multiple React components. So the idea ...
- [React] Implement a Higher Order Component with Render Props
When making a reusable component, you'll find that people often like to have the API they're most fa ...
- [React Intl] Use a react-intl Higher Order Component to format messages
In some cases, you might need to pass a string from your intl messages.js file as a prop to a compon ...
- [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 ...
- simple python code when @ simplnano
code: import serial,time,itertools try: ser=serial.Serial(2,115200,timeout=0) except: print 'Open CO ...
随机推荐
- kafka条件查询excel拼接
1 SELECT COUNT(*) FROM wiseweb_crawler_metasearch_page20171214 WHERE (content like '%内蒙古%'or content ...
- Hibernate 4.3 配置文件实现
1.建立web项目 2.复制相关的jar文件到 项目的lib目录下antlr-2.7.7.jardom4j-1.6.1.jarhibernate-commons-annotations-4.0.5.F ...
- 任务12:Bind读取配置到C#实例
将json文件的配置转换成C#的实体 新建项目: Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definition ...
- DFS系列 POJ(自认为的讲解)
C - Sum It Up POJ1564 题意: 给你一个N,然后给你一堆数The numbers in each list appear in nonincreasing order, and t ...
- IEnumerable<T> 的时候一个主意事项p
IEnumerator IEnumerable.GetEnumerator() { return _vtDataView.GetEnumerator(); } public IEnumerator&l ...
- bzoj 1488: [HNOI2009]图的同构【polya定理+dfs】
把连边和不连边看成黑白染色,然后就变成了 https://www.cnblogs.com/lokiii/p/10055629.html 这篇讲得好!https://blog.csdn.net/wzq_ ...
- 洛谷 P2296 寻找道路【bfs+spfa】
反向建边bfs出不能到t的点,然后对每个能到这些点的点打上del标记,然后spfa的时候不经过这些点即可 #include<iostream> #include<cstdio> ...
- springboot(十二) SpringBoot 性能优化
代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo springboot优化主要有三类优化:1.包扫描优化 2. ...
- JAVA多线程(一) Thread & Runnable
githut代码地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service/ ...
- update cdh version ,but cdh use old conf ,problem solve
最近升级cdh版本,从4.5 升级到 5.0.0 beta-2 但是升级后,发现/etc/alternatives 路径下的软链接还是只想旧的4.5 版本,而且hadoop环境也是沿用4.5 的版本c ...