函数式编程(九)——map,filter,reduce
编程方法论:
面向过程:按照一个固定的流程去模拟解决问题的流程
函数式:编程语言定义的函数 + 数学意义的函数
y = 2*x + 1 函数用编程语言实现
def fun(x):
return 2*x + 1
面向对象:
函数式编程
1. 不可变:不用变量保存状态,不可修改变量
# 函数式编程
# 1. 不可变:不用变量保存状态,不可修改变量 # 非函数式
a = 1
def test1():
global a
a += 1
return a # 函数式
n = 1
def test2():
return n + 1
2.函数即“变量”
a.函数可以当参数传递
b.返回值可以式函数名
def foo(n):
print(n) def bar(name):
print('my name is %s' % name) foo(bar)
foo(bar('zhangsan'))
输出:
<function bar at 0x0000024E4B385C80>
my name is zhangsan
None
3.高阶函数 :1.函数接受的参数是一个函数名 2.返回值是函数名,两个条件满足一个就是高阶函数
# 把函数当中参数传给另外一个函数 def foo(n):
print(n) def bar(name):
print('my name is %s' % name) foo(bar)
foo(bar('zhangsan')) # 返回值中包含函数 def test3():
print('from test3') def handle():
print('from handle')
return test3() handle()
4.尾调用
在函数的最后一步调用另外一个函数(最后一行不一定是最后一步),调用函数的栈状态不需要保存,可以用来优化递归函数,俗称:尾递归
map函数
# map函数 # num_l = [1, 2, 3, 4] #求平方
# ret = []
# for i in num_l:
# ret.append(i**2)
# print(ret) def add_one(x):
return x + 1 def reduce_one(x):
return x - 1 def pingfang(x):
return x**2 def map_test(func, array):
ret = []
for i in array:
res = func(i)
ret.append(res)
return ret num_2 = [1, 2, 3, 4]
ret = map_test(add_one, num_2)
print(ret)
ret = map_test(reduce_one, num_2)
print(ret)
ret = map_test(pingfang, num_2)
print(ret)
ret = map_test(lambda x:x+2, num_2) # lambda方式
print(ret) res = map(lambda x: x + 2, num_2) # 内置map函数 ,返回可迭代对象
print(res) # <map object at 0x000001F62514BF60>
print(list(res)) # [3, 4, 5, 6]
print(list(map(reduce_one, num_2))) # [0, 1, 2, 3] msg = 'hello'
print(list(map(lambda x: x.upper(), msg)))
filter函数
# filter
people = ['sb_A', 'sb_B', 'C_sb', 'D_sb'] def end_with_sb(n):
return n.endswith('sb') def start_with_sb(n):
return n.startswith('sb') def filter_test1(array):
res1 = []
for p in array:
if not p.startswith('sb'):
res1.append(p)
return res1 def filter_test2(fun, array):
res1 = []
for p in array:
if not fun(p):
res1.append(p)
return res1 print(filter_test1(people))
print(filter_test2(start_with_sb, people))
print(filter_test2(end_with_sb, people))
print(filter_test2(lambda n1: n1.endswith('sb'), people))
print('*'*20)
ret2 = filter(lambda n2: n2.endswith('sb'), people) # 内置函数,返回一个内存地址,保存了list地址
print(list(ret2)) # ['C_sb', 'D_sb']
ret3 = filter(lambda n2: not n2.endswith('sb'), people)
# lambda n2: n2.endswith('sb') 返回一个bool值,为true则保留
print(list(ret3)) # ['sb_A', 'sb_B']
info = [{'name': 'a', 'score': 80},
        {'name': 'b', 'score': 90},
        {'name': 'c', 'score': 90}]
res = filter(lambda i: i['score'] < 90, info)
print(list(res)) # [{'name': 'a', 'score': 80}
reduce函数
# reduce函数
num_3 = [1, 2, 3, 100] def reduce_test(fun, array, init=None):
if init is None:
res = array.pop(0)
else:
res = init
for num in array:
res = fun(res,num)
return res res = reduce_test(lambda x, y: x*y, num_3)
print(res) #
res = reduce_test(lambda x, y: x*y, num_3, 10)
print(res) # from functools import reduce
num_4 = [1, 2, 3, 100]
res = reduce(lambda x, y: x+y, num_4, 10)
print(res) #
函数式编程(九)——map,filter,reduce的更多相关文章
- Python函数式编程中map()、reduce()和filter()函数的用法
		Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和“行内函数”lambda函数 ... 
- Python之路Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数
		Python之路Python作用域.匿名函数.函数式编程.map函数.filter函数.reduce函数 一.作用域 return 可以返回任意值例子 def test1(): print(" ... 
- 函数式编程工具:filter和reduce
		# -*- coding: utf-8 -*- #python 27 #xiaodeng #函数式编程工具:filter和reduce #python内置函数中,map函数是用来进行函数式编程这类工具 ... 
- python 内置函数 map filter reduce lambda
		map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ... 
- 如何在python3.3用 map filter reduce
		在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ... 
- Swift map filter reduce 使用指南
		转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ... 
- python常用函数进阶(2)之map,filter,reduce,zip
		Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ... 
- 数组的高阶方法map filter reduce的使用
		数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ... 
- python学习-day16:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r
		一.作用域 作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变 二.匿名函数 lambda:正常和其他函数进行配合使用.正常无需把匿名函数赋值给一个变量. f=lambda x:x*x p ... 
随机推荐
- Ubuntu16.04下安装QQ的完整操作记录(经验证可用)
			本机安装了Ubuntu16.04系统,用于日常运维办公.打算在Ubuntu上安装QQ,如下操作记录也使用于Ubuntu18.04: 1)先下载特制的QQ程序包(其实就是基于Wine容器做了一些封装,程 ... 
- 【Beta阶段】第九次Scrum Meeting!(论坛已成功上线)
			每日任务内容: 本次会议为第九次Scrum Meeting会议~ 本次会议为团队项目第九次会议,在会议前大家取得了重大成果! 队员 昨日完成任务 明日要完成任务 刘乾 #179 完成1021的数据处理 ... 
- Maven项目中添加JDBC驱动
			在pom.xml配置文件中添加: <dependency> <groupId>mysql</groupId> <artifactId>mysql-con ... 
- node的读写流
			let http = require('http'); http.createServer((req,res)=>{ res.end(); }).listen(,()=>{ console ... 
- Intel SP处理机以及AMD处理器的一些对比资料
			1. EPYC 与 intel的CPU对比 2. Intel SP处理器参数: 3. AMD处理器参数 4. Intel SP处理器部分参数2 5. Intel SP处理器简单说明 6. intel ... 
- ionic3应用的Android打包签名发布步骤
			版权声明:本文为博主原创文章,未经博主允许不得转载. 当我们的ionic应用开发结束之后,就要开始上线到应用市场,那么Android的发布步骤具体是怎么样的呢? 1)编译 运行命令行: ionic c ... 
- MySQL分区管理
			以下是我看MySQL官方文档的时候整理的笔记,仅作参考保留. RANGE,LIST分区管理 1:为未分区表创建分区 ; 2:删除某个分区的数据 ALTER TABLE tr DROP PARTITIO ... 
- [代码]Delphi实现窗体内嵌其他应用程序窗体
			实现原理是启动一个应用程序,通过ProcessID得到窗体句柄,然后对其设定父窗体句柄为本程序某控件句柄(本例是窗体内一个Panel的句柄),这样就达成了内嵌的效果. 本文实现的是内嵌一个记事本程序, ... 
- Lodop设置文本项行间距、字间距
			LODOP给文本项ADD_PRINT_TEXT设置字间距.行间距,可以在打印设计页面,右键属性里设置,然后在打印设计生成代码,也可以直接写代码.LineSpacing行间距.LetterSpacing ... 
- 微软开放 6 万项 Linux 专利,有哪些是我们该注意的?
			导读 上周,微软宣布正式加入 Open Invention Network (“OIN”) 社区,开放其 6 万多项 Linux 专利.消息一出,许多人疑惑微软为什么要这么做?作为普通开发者,是否能使 ... 
