函数式编程(九)——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 ...
随机推荐
- 免费的 Vue.js 入门与进阶视频教程
这是我免费发布的高质量超清「Vue.js 入门与进阶视频教程」. 全网最好的.免费的 Vue.js 视频教程,课程基于 Vue.js 2.0,由浅入深,最后结合实际的项目进行了最棒的技术点讲解,此课程 ...
- Ubuntu16.04下安装破解secureCRT和secureFX的操作记录
本地电脑之前安装的是win10,疲于win10频繁的更新和各种兼容问题,果断放弃win10系统,安装了Ubuntu 16.04系统,现在微信.QQ.钉钉.WPS等都已支持linux版本,所以在Ubun ...
- Redis常用操作-------List(列表)
1.BLPOP key [key ...] timeout BLPOP 是列表的阻塞式(blocking)弹出原语. 它是 LPOP 命令的阻塞版本,当给定列表内没有任何元素可供弹出的时候,连接将被 ...
- 安装Visual Studio开发平台
1.找一个VS2013的安装包,下载到D盘上,勾选相应的选项安装. 安装的过程很漫长,至少需要一个小时. 2.安装已完成,启动. . 3.登录. \ 4启动VS2013. 5.新建c#类库 6.输入代 ...
- #个人博客作业Week3——必应词典案例分析
第一部分 调研以及评测 一.BUG分析 1. 翻译部分原文语言检测部分 1) 症状: 当选择原文语言是简体中文时,输入英文查询,程序不报错,继续翻译,选择其他类型语言也是如此. 且如果出现这种情况 ...
- 第二个spring, 第7天
陈志棚:成绩的统筹 李天麟:界面音乐 徐侃:代码算法 代码初步已经完成.还差最后一步整合.附上最后一张截图
- python中的文件读写(open()函数、with open('file_directory','r') as f:、read()函数等)
python中也有文件读写,通过调用内置的读写函数.可以完成文件的打开/关闭.读.写入.追加等功能. open()函数 open()函数为python中的打开文件函数,使用方式为: f = open( ...
- Install Jetty web server on CentOS 7 / RHEL 7
http://www.eclipse.org/jetty/download.html http://www.eclipse.org/jetty/documentation/current/startu ...
- MySQL乐观锁在分布式场景下的实践
背景 在电商购物的场景下,当我们点击购物时,后端服务就会对相应的商品进行减库存操作.在单实例部署的情况,我们可以简单地使用JVM提供的锁机制对减库存操作进行加锁,防止多个用户同时点击购买后导致的库存不 ...
- Delphi中根据分类数据生成树形结构的最优方法
一. 引言: TreeView控件适合于表示具有多层次关系的数据.它以简洁的界面,表现形式清晰.形象,操作简单而深受用户喜爱.而且用它可以实现ListView.ListBox所无法实现的很多功能 ...