编程方法论:

面向过程:按照一个固定的流程去模拟解决问题的流程

函数式:编程语言定义的函数 + 数学意义的函数

    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的更多相关文章

  1. Python函数式编程中map()、reduce()和filter()函数的用法

    Python中map().reduce()和filter()三个函数均是应用于序列的内置函数,分别对序列进行遍历.递归计算以及过滤操作.这三个内置函数在实际使用过程中常常和“行内函数”lambda函数 ...

  2. Python之路Python作用域、匿名函数、函数式编程、map函数、filter函数、reduce函数

    Python之路Python作用域.匿名函数.函数式编程.map函数.filter函数.reduce函数 一.作用域 return 可以返回任意值例子 def test1(): print(" ...

  3. 函数式编程工具:filter和reduce

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #函数式编程工具:filter和reduce #python内置函数中,map函数是用来进行函数式编程这类工具 ...

  4. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  5. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  6. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  7. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  8. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  9. python学习-day16:函数作用域、匿名函数、函数式编程、map、filter、reduce函数、内置函数r

    一.作用域 作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变 二.匿名函数 lambda:正常和其他函数进行配合使用.正常无需把匿名函数赋值给一个变量. f=lambda x:x*x p ...

随机推荐

  1. nginx的access.log文件详解

    事实证明,日志文件真的是很重要很重要的.能够帮助我们快速的定位问题,并且知道用户访问的状态,浏览器,Ip,接口地址等,简直可怕.. 一.nginx的access.log(1)对博主而言,日志文件存放在 ...

  2. python基础学习笔记(三)

    序列概览 Python 包含6 种内建的序列,这里重点讨论最常用的两种类型:列表和元组. 列表与元组的主要区别在于,列表可以修改,元组则不能.也就是说如果要根据要求来添加元素,那么列表可以会更好用:而 ...

  3. HDU 3537 Daizhenyang's Coin

    链接 [http://acm.hdu.edu.cn/showproblem.php?pid=3537] 题意 题意:已知一排硬币中有n个硬币正面朝上,输入正面朝上的硬币的位置ai.两人轮流操作, 每次 ...

  4. Linux实践三:程序破解

    一.汇编指令机器码 二.反汇编与十六进制编程器 三.可执行文件的基本格式 hexdump -x login 用16进制数字显示login内容 objdump -x login 显示login中各个段以 ...

  5. Rabbitmq vs. kafka

    https://mp.weixin.qq.com/s/2i_9TWoF3TsJvG6Dj_75vw http://www.cnblogs.com/valor-xh/p/6348009.html htt ...

  6. Ehcache配置参数示例

    从Ehcache的jar包里抽取的 <!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more con ...

  7. [转帖]以Windows服务方式运行.NET Core程序

    以Windows服务方式运行.NET Core程序 原作者blog:https://www.cnblogs.com/guogangj/p/10093102.html 里面使用了NSSM 工具 但是自己 ...

  8. 转帖 Oracle 主键的处理方法 http://www.cnblogs.com/Richardzhu/p/3470929.html

    Oracle之主键的创建.添加.删除操作   一.创建表的同时创建主键约束 1.1.无命名 SQL> create table jack (id int primary key not null ...

  9. char类型的数值转换

    在视频教程中,你已经认识到了数字类型之间.字符串和其他类型之间的转换.而某些时候,我们还需要将char类型转换为int类型,或者把int类型转换为char类型. 这篇文章,将介绍在代码中虽然不太常用, ...

  10. ceph radosgw-admin的操作

    常用操作: 生成一新用户: 在两个集群当中都创建相同的管理用户 radosgw-admin user create --uid=admin --display-name=admin --access_ ...