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 ...
随机推荐
- Linux 下WAS的java版本查看
1.查找linux的详细版本号: A.cat /proc/version B.lsb_release -a(可以查出是否为redhat开发的) C.uname -a 2.Linux的java版本 A. ...
- 第五届蓝桥杯C++B组省赛
1.啤酒和饮料 2.切面条 3.李白打酒 4.史丰收速算 5.打印图形 6.奇怪的分式 7.六角填数 8.蚂蚁感冒 9.地宫取宝 10.小朋友排队
- Android 增,删,改,查 通讯录中的联系人
一.权限 操作通讯录必须在AndroidManifest.xml中先添加2个权限, <uses-permission android:name="android.permission. ...
- 频繁GC会造成卡顿
频繁GC会造成卡顿 https://www.cnblogs.com/qcloud1001/p/9525078.html 一款app除了要有令人惊叹的功能和令人发指交互之外,在性能上也应该追求丝滑的要求 ...
- npm 基本命令行
npm是随同NodeJS一起安装的包管理工具. 检查版本 npm -v 升级npm npm install npm -g 使用淘宝镜像升级 cnpm install npm -g npm instal ...
- Centos_svn安装操作使用步骤
安装svn服务器端 (1)从镜像下载安装svn服务器端yum install subversion//进入目录,准备创建svn目录cd /usr/local///创建一个svn目录mkdir svn/ ...
- linux中用无名管道进行文件的读写
1管道是什么: 水管子大家知道,有两端,在此一端用来读一端用来写,其中一端的输出作为另外一端的输入. 2 函数原型 int pipe(int pipefd[2]);//参数中分别代表的两端 3 例子: ...
- UVa 1643 Angle and Squares (计算几何)
题意:有n个正方形和一个角(均在第一象限中),使这些正方形与这个角构成封闭的阴影区域,求阴影区域面积的最大值. 析:很容易知道只有所有的正方形的对角形在一条直线时,是最大的,然后根据数学关系,就容易得 ...
- Google Play应用商店的下载路径(转载)
转自:http://blog.yanwen.org/archives/1660.html 其实,好久之前就想知道google play中下载的应用到哪里去了.之前用的MIUI系统里面,google p ...
- JAVA基础--异常10
一.Object类简介 1.Object类简介 Object,是Java一切对象的最高父类:如果一个类没有显示继承一个类,他的父类就是Object: 它描述的是Java世界里所有对象最最共性的信息,它 ...