以下为学习笔记:来自廖雪峰的官方网站

1.高阶函数:简单来说是一个函数里面嵌入另一个函数

2.python内建的了map()和reduce()函数

map()函数接收两参数,一个是函数,一个是Iterable.    map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterable返回

例子:

#map函数
def f(x):
return x * x
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) #map函数返回一个函数和一个Iterable
print r
#将数字作为字符串输出

print(map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9]))

reduce()函数:把一个函数作用在一个序列【x1,x2,x3,...】上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累计计算,其效果就是
reduce(f,[x1,x2,x3,x4])=f(f(f(x1,x2),x3),x4) 3.练习题1:将名字规范化
def normalize(name):   #规范名字的写法
name = name[0].upper() + (name[1:]).lower()
return name r = map(normalize, ['LISA', 'adam', 'BaRt'])
print r
练习题2:#python提供sum()函数可以接受一个list并求和,请编写一个prod()函数,可以接受一个list,并求和
from functools import reduce l = [3, 5, 7, 9]
def prod(a, b):
return a * b print(reduce(prod, l))
练习题3.#利用map和reduce编写一个str2float函数,把字符串‘123.456’变成123.456
from functools import reduce def char2num(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s] def str2float(s):
s = s.split('.')
if len(s[0]) == 0:
s[0] = '0'
return (reduce(lambda x, y: x * 10 + y, map(char2num, s[0]))) + \
(reduce(lambda x, y: x * 10 + y, map(char2num, s[1]))) * pow(0.1, len(s[1])) print(str2float('123.456'))

python之高阶函数--map()和reduce()的更多相关文章

  1. [ Python - 9 ] 高阶函数map和reduce连用实例

    1. 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: from functools import reduce def str2num( ...

  2. Python高阶函数map、reduce、filter、sorted的应用

    #-*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.support.wait import Web ...

  3. python之高阶函数map/reduce

    L = [] for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]: L.append(f(n)) print(L) Python内建了map()和reduce()函数. 我们先看 ...

  4. python学习笔记1 -- 函数式编程之高阶函数 map 和reduce

    我用我自己,就是高阶函数,直接表现就是函数可以作为另一个函数的参数,也可以作为返回值 首先一个知识点是 函数的表现形式,印象中的是def  fw(参数)这种方式定义一个函数 python有很多的内置函 ...

  5. python的高阶函数(map,filter,sorted,reduce)

    高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...

  6. 高阶函数map(),filter(),reduce()

    接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...

  7. js高阶函数map和reduce

    map 举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组[1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map实现如下: 由于map()方法定义在JavaScr ...

  8. 高阶函数-map/filter/reduce

    什么样的函数叫高阶函数: 条件:1.函数接受函数作为参数 2.函数的返回值中包含函数 高阶函数之----map函数 map(func, *iterables) --> map objectnum ...

  9. JS高阶函数--------map、reduce、filter

    一.filter filter用于对数组进行过滤.它创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() ...

随机推荐

  1. C++【string】用法和例子

    /*** * string 基础api复习 * 8 AUG 2018 */ #include <iostream> #include <string> using namesp ...

  2. Chapter 5 查找

    Chapter 5 查找 1-   顺序查找法 O(n) 2-   折半查找O(logn) :二分查找 要求:关键字有序 过程: 判定树:叶子结点为方框,代表不成功的结点. 3-   分块查找:索引顺 ...

  3. Compile_Netgen_WITH_OpenCascade

    title: Compile_Netgen_WITH_OpenCascade date: 2016-04-23 21:14:42 tags: 结合OCCT编译Netgen date: 2016-04- ...

  4. tp5.1 swoole 实现异步处理

    客户端请求:<?phpnamespace app\index\controller; class Index{ public function index() { $client = new \ ...

  5. Java基础——List集合整理(脑图,源码,面试题)

    常在知乎牛客网关注Java的一些面试,了解过校招社招常面哪些内容.Java集合不仅使用频率高而且在初面中也常常被问到,何止是常常,关于ArrayList的扩容,HashMap的一些底层等等都被问到烂了 ...

  6. 组件:slot插槽

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  7. PAT甲级——A1023 Have Fun with Numbers

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...

  8. Hibernate-一对多|多对一-多对多

    1 一对多|多对一 1.1 关系表达 表中的表达 表中的表达  实体中的表达 orm元数据中表达 一对多 多对一 1.2 操作 操作关联属性 1.3 进阶操作 级联操作 结论: 简化操作.一定要用,s ...

  9. TZOJ 4267 An Easy Puz(深搜)

    描述 Wddpdh find an interesting mini-game in the BBS of WHU, called “An easy PUZ”. It’s a 6 * 6 chess ...

  10. 设置mysql二进制日志过期时间

    ((none)) > show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | ...