一、函数的作用域

1、作用域在定义函数时就已经固定住了,不会随着调用位置的改变而改变

例一:
name='alex' def foo():
name='lhf'
def bar():
print(name)
return bar func=foo()
func() 例二:
name='alex' def foo():
name='lhf'
def bar():
name='wupeiqi'
def tt():
print(name)
return tt
return bar func=foo()
func()()
foo()()()

二、匿名函数

#匿名函数
#格式:lambda关键字 形参:表达式
fun=lambda x:x+1
print(fun(3))

 

name='张鹏'
fun=lambda x:x+'帅锅'
red=fun(name)
print(red)

 

name='name'
funn=lambda x,y:(x.startswith('n'),y+'1')
print(funn('knnnn','dddddd'))

三、函数式编程

函数的参数传入,是函数吃进去的食物,而函数return的返回值,是函数拉出来的结果,

面向过程的思路就是,把程序的执行当做一串首尾相连的函数,一个函数吃,拉出的东西给另外一个函数吃,另外一个函数吃了再继续拉给下一个函数吃。。。

1、不可变,不用变了保存状态,不修改变量

2、第一类对象:函数即‘变量’

  函数名可以当参数传递

  返回值可以是函数名
例如:
用户登录流程:前端接收处理用户请求-》将用户信息传给逻辑层,逻辑词处理用户信息-》将用户信息写入数据库
验证用户登录流程:数据库查询/处理用户信息-》交给逻辑层,逻辑层处理用户信息-》用户信息交给前端,前端显示用户信息

函数式编程:

(以下部分内容摘自网络)

11 高阶函数

满足俩个特性任意一个即为高阶函数

1.函数的传入参数是一个函数名(把函数当做参数,传给另外一个函数;)

 def foo (n):
print(n )
def bar(name):
print('my name is %s'%name)
foo(bar('张三'))

2.函数的返回值是一个函数名(返回值中包含函数)

尾调用

map()函数

array=[1,3,4,71,2]

ret=[]
for i in array:
ret.append(i**2)
print(ret)

#如果我们有一万个列表,那么你只能把上面的逻辑定义成函数
def map_test(array):
ret=[]
for i in array:
ret.append(i**2)
return ret

print(map_test(array))

#如果我们的需求变了,不是把列表中每个元素都平方,还有加1,减一,那么可以这样
def add_num(x):
return x+1
def map_test(func,array):
ret=[]
for i in array:
ret.append(func(i))
return ret

print(map_test(add_num,array))
#可以使用匿名函数
print(map_test(lambda x:x-1,array))

#上面就是map函数的功能,map得到的结果是可迭代对象
print(map(lambda x:x-1,range(5)))

  map函数

array=[1,3,4,71,2]

ret=[]
for i in array:
ret.append(i**2)
print(ret) #如果我们有一万个列表,那么你只能把上面的逻辑定义成函数
def map_test(array):
ret=[]
for i in array:
ret.append(i**2)
return ret print(map_test(array)) #如果我们的需求变了,不是把列表中每个元素都平方,还有加1,减一,那么可以这样
def add_num(x):
return x+1
def map_test(func,array):
ret=[]
for i in array:
ret.append(func(i))
return ret print(map_test(add_num,array))
#可以使用匿名函数
print(map_test(lambda x:x-1,array)) #上面就是map函数的功能,map得到的结果是可迭代对象
print(map(lambda x:x-1,range(5)))

  

filter()函数
move_people=['zp','sb','sb_ls','alex']
ret=[]
for p in move_people:
if not p.startswith('sb'):
ret.append(p)
print(ret)
move_people1=['sgzp','sb','sb_lssb','sbalexsb','sbooppp','sssddhfhfhhdhsb']
"""
开始位置查询或排除
"""
def sb_(p):
return p.endswith('sb')
'''
结尾处查找或排除
'''
def _sb(p):
return p.startswith('sb')
'''
总的调用
'''
def save(area,func):
ret = []
for p in area:
if not func(p):
ret.append(p)
return ret
n=save(move_people1,sb_)
print('好人1:',n)
n=save(move_people1,_sb)
print('好人2:',n)
'''终极版
'''
n=save(move_people1,lambda x: p.startswith('sss'))
print('好人3:',n)
print('好人4',list(filter(lambda p:not p.endswith('hsb'),move_people1)))#move_people1是一个可迭代对象

 reduce()函数

 from functools import reduce
#合并,得一个合并的结果
array_test=[1,2,3,4,5,6,7]
array=range(100) #报错啊,res没有指定初始值
def reduce_test(func,array):
l=list(array)
for i in l:
res=func(res,i)
return res # print(reduce_test(lambda x,y:x+y,array)) #可以从列表左边弹出第一个值
def reduce_test(func,array):
l=list(array)
res=l.pop(0)
for i in l:
res=func(res,i)
return res print(reduce_test(lambda x,y:x+y,array)) #我们应该支持用户自己传入初始值
def reduce_test(func,array,init=None):
l=list(array)
if init is None:
res=l.pop(0)
else:
res=init
for i in l:
res=func(res,i)
return res print(reduce_test(lambda x,y:x+y,array))
print(reduce_test(lambda x,y:x+y,array,50)) reduce函数

 总结

map()
处理序列中的每个元素,得到的结果是一个’列表‘,该‘列表’元素个数及位置与原来一样
filter()
遍历序列中的每个元素,得到的每个元素得到的布尔值,如果是true则保留下来
reduce()
处理一个序列,然后将序列合并操作
 #当然了,map,filter,reduce,可以处理所有数据类型

 name_dic=[
{'name':'alex','age':1000},
{'name':'wupeiqi','age':10000},
{'name':'yuanhao','age':9000},
{'name':'linhaifeng','age':18},
]
#利用filter过滤掉千年王八,万年龟,还有一个九千岁
def func(x):
age_list=[1000,10000,9000]
return x['age'] not in age_list res=filter(func,name_dic)
for i in res:
print(i) res=filter(lambda x:x['age'] == 18,name_dic)
for i in res:
print(i) #reduce用来计算1到100的和
from functools import reduce
print(reduce(lambda x,y:x+y,range(100),100))
print(reduce(lambda x,y:x+y,range(1,101))) #用map来处理字符串列表啊,把列表中所有人都变成sb,比方alex_sb
name=['alex','wupeiqi','yuanhao'] res=map(lambda x:x+'_sb',name)
for i in res:
print(i)

总结

最新文章

  1. “fatal error C1010”错误解决的三种方法
  2. [Scala] Scala基础知识
  3. 转:HTTP 1.1与HTTP 1.0的比较
  4. iOS 定位精度
  5. 四则运算2扩展---c++
  6. NFC(9)NDEF文本格式规范及读写示例(解析与封装ndef 文本)
  7. CRC32校验的用法
  8. C 程序提升效率的10种方法
  9. HDU 4983 Goffi and GCD
  10. JS数组追加数组採用push.apply的坑
  11. 在原有数据库中使用 CodeFirst
  12. 【重磅】Spring Boot 2.0权威发布
  13. 4--Postman--Request&Response
  14. this 相关
  15. elasticsearch best_fields most_fields cross_fields从内在实现看区别——本质就是前两者是以field为中心,后者是词条为中心
  16. ACM找bug方案
  17. json数据格式 net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of Error:(findColumns1)Read timed out
  18. BI 可视化
  19. [转]文件后缀与Mime类型对照表
  20. UVa 10766 Organising the Organisation(矩阵树定理)

热门文章

  1. debug --- 使用Eclipse
  2. :last-child的坑-CSS3选择器
  3. 2019ICPC南京网络赛总结
  4. 【NOIP2017提高A组模拟9.5】心灵治愈
  5. ASP.NET通过反射生成sql语句
  6. redis安装成功后get: command not found
  7. Yarn 内存分配管理机制及相关参数配置
  8. [CSP-S模拟测试]:求和(数学)
  9. ThreadPool用法与优势
  10. 后盾网lavarel视频项目---lavarel使用模型进行增删改查操作