函数式编程(九)——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 ...
 
随机推荐
- 完整部署CentOS7.2+OpenStack+kvm 云平台环境(2)--云硬盘等后续配置
			
继上一篇博客介绍了完整部署CentOS7.2+OpenStack+kvm 云平台环境(1)--基础环境搭建,本篇继续讲述后续部分的内容 1 虚拟机相关1.1 虚拟机位置介绍 openstack上创建的 ...
 - HttpServletResponse类学习
			
/*//2) 乱码的解决. //设置服务器输出的编码为UTF-8---在BaseServlet处已经已经进行了设置 response.setCharacterEncoding("UTF-8& ...
 - 开源RabbitMQ操作组件
			
开源RabbitMQ操作组件 对于目前大多的.NET项目,其实使用的技术栈都是差不多,估计现在很少用控件开发项目的了,毕竟一大堆问题.对.NET的项目,目前比较适合的架构ASP.NET MVC,ASP ...
 - shell脚本--权限分配
			
因为shell脚本内部是很多命令的集合,这些命令也许会涉及到操作某一个文件,而且shell脚本的运行,也是需要当前用户对脚本具有运行的权限,否则,会因为权限不够而失败. 首先最重要的一点:修改权限,只 ...
 - PAT 1083 是否存在相等的差
			
https://pintia.cn/problem-sets/994805260223102976/problems/994805260780945408 给定 N 张卡片,正面分别写上 1.2.…… ...
 - Spring Cloud 路由网关服务端
			
修改application.properties配置文件:服务端口号.本机名称: 启动注册中心:java -jar uap-register-server-1.0.jar --spring.confi ...
 - VUE的语法笔记
			
v-model = 'content' {{contents}} //vue 双向视图的绑定 v-text 只能返回一个文本内容 v-html 不仅可以返回文本内容还可以返回html标签 v-for ...
 - 软件工程_6th weeks
			
一.上次博客时说的UI,拖拉到现在才展示,完成了“登录,普通匹配,做题界面,做题结果”四项 功能: 二.单元测试工具 1.python单元测试工具 最近因为论文原因一直在用Python,Pytho ...
 - 几种实现one-hot编码的方式
			
方法1 之前写使用sklearn进行数据挖掘-房价预测(4)-数据预处理一文中处理标签类特征时候已经提到过,使用sklearn中提供的LabelEncoder和OneHotEncoder方法 a = ...
 - Delphi中根据分类数据生成树形结构的最优方法
			
一. 引言: TreeView控件适合于表示具有多层次关系的数据.它以简洁的界面,表现形式清晰.形象,操作简单而深受用户喜爱.而且用它可以实现ListView.ListBox所无法实现的很多功能 ...