python之高阶函数--map()和reduce()
以下为学习笔记:来自廖雪峰的官方网站
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()的更多相关文章
- [ Python - 9 ] 高阶函数map和reduce连用实例
1. 利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456: from functools import reduce def str2num( ...
- Python高阶函数map、reduce、filter、sorted的应用
#-*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.support.wait import Web ...
- 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()函数. 我们先看 ...
- python学习笔记1 -- 函数式编程之高阶函数 map 和reduce
我用我自己,就是高阶函数,直接表现就是函数可以作为另一个函数的参数,也可以作为返回值 首先一个知识点是 函数的表现形式,印象中的是def fw(参数)这种方式定义一个函数 python有很多的内置函 ...
- python的高阶函数(map,filter,sorted,reduce)
高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...
- 高阶函数map(),filter(),reduce()
接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions. map()和filter()是内置函数.在python3中,reduce()已不再是 ...
- js高阶函数map和reduce
map 举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组[1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map实现如下: 由于map()方法定义在JavaScr ...
- 高阶函数-map/filter/reduce
什么样的函数叫高阶函数: 条件:1.函数接受函数作为参数 2.函数的返回值中包含函数 高阶函数之----map函数 map(func, *iterables) --> map objectnum ...
- JS高阶函数--------map、reduce、filter
一.filter filter用于对数组进行过滤.它创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() ...
随机推荐
- 二分判定 覆盖问题 BZOJ 1052
//二分判定 覆盖问题 BZOJ 1052 // 首先确定一个最小矩阵包围所有点,则最优正方形的一个角一定与矩形一个角重合. // 然后枚举每个角,再解决子问题 #include <bits/s ...
- webpack 打包生成的index 路径引用不对
webpack 在打包时在访问打包里面的index时,出现路径错误 修改方法为 解决方法:在config下面的index.js把 assetsPublicPath: '/', 修改为: assetsP ...
- 如何将Map键值的下划线转为驼峰
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:如何将Map键值的下划线转为驼峰: 例,将HashMap实例extMap键值下划线转为驼峰: 代码: HashMap<String ...
- webapp中<meta>与css代码部署
1.页面头部标签申明 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" id="te ...
- JZOJ5894【NOIP2018模拟10.5】同余方程
题目 Description
- php对输入的检测
$data['value'] = trim(stripslashes(htmlspecialchars_decode($value)));
- PAT甲级——A1038 Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- WIN10 Internet协议版本tcp/ipv4属性灰色没有法点击
问题图如下: 1.右键打开“计算机”点击菜单,选中“管理”: 2.进入计算机管理后,打开“设备管理器”,然后在右侧打开点击“网络适配器”,如果有驱动的话,在驱动上右键点击菜单,选中卸载: 3.卸载网卡 ...
- eclipse中运行java程序
1 package ttt; public class Testttt { public static void main() { Person p =new Person(); p.name=&qu ...
- webservice作用(优,缺点;作用)
1其实我们平时的应用,有一方面考虑是部署方便,维护容易~!如果是DLL,部署,更新需要每个应用了这个DLL的应用程序都作相应的引用更新...而如果用了Ws,则不用,因为它通过网络部署,通过网络引用,基 ...