编程方法论:

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

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

    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. vue全局 关键字搜索 v-search

    一款基于 vuejs & weui 的全屏搜索组件:https://www.npmjs.com/package/vue-search

  2. Linux内核及分析 第八周 进程的切换和系统的一般执行过程

    学习笔记: 一.进程调度与进程调度的时机分析 1.不同类型的进程有不同需求的调度需求: 第一种分类: —I/O-bound:频繁的进行I/O,通常会花费很多时间等待I/O操作的完成 —CPU-boun ...

  3. Linux内核分析——期中总结

    期中总结 一.MOOC课程 (一)计算机是如何工作的 1.冯诺依曼体系结构的核心思想是存储程序计算机. 2.CPU在实际取指令时根据cs:eip来准确定位一个指令. 3.寄存器模式,以%开头的寄存器标 ...

  4. Android之JSON格式数据解析

    查看原文:http://blog.csdn.net/hantangsongming/article/details/42234293 JSON:JavaScript 对象表示法(JavaScript ...

  5. 关于QQ的NABCD模型

    关于QQ的NABCD模型 N--Need 随着电脑的普及,人们在网络上进行交流的时间越来越多,由于现有的交流工具还不是那么的完善,还不能够完全满足人们在交流时的需求.因此为了满足人们更多的需求,我们设 ...

  6. Sprint第三个计划

    这一次是最后的一个阶段,承上启下.这一阶段我们将转向Android的主要设计.加油,最后十天.

  7. Fortify Scan - Static Code Analyzer

    https://software.microfocus.com/en-us/products/application-security-testing/overview https://softwar ...

  8. ubuntu 下搭建redis和php的redis的拓展

    系统环境: 腾讯云服务器, ubuntu16.0.4.4 ,php7.0   一.安装redis服务 sudo apt-get install redis-server 安装好的redis目录在 /e ...

  9. 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看)

    前两天朋友发给我了一篇文章,是携程网反爬虫组的技术经理写的,大概讲的是如何用他的超高智商通过(挑衅.怜悯.嘲讽.猥琐)的方式来完美碾压爬虫开发者.今天我就先带大家开发一个最简单低端的爬虫,突破携程网超 ...

  10. PSP(4.20——4.26)以及周记录

    1.PSP 4.20 8:45 9:25 10 30 Cordova A Y min 13:00 17:00 65 175 Cordova A Y min 4.21 9:00 17:00 125 35 ...