码迷,mamicode.com
首页 > 其他好文 > 详细

函数(作用域,匿名函数,函数式编程,高阶函数)

时间:2019-11-01 22:32:25      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:color   pop   包含   声明   col   函数名   函数式编程   内存地址   迭代器   

一.函数作用域                                          
1.函数名表示的是内存地址
1 def test1():
2     print(in the test1)
3 def test():
4     print(in the test)
5     return test1
6 
7 print(test())
打印:
in the test
<function test1 at 0x000001E90E452EA0>

2.函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
1 name = xiaopang
2 def foo():
3     name=fangjiyi
4     def bar():
5 
6         print(name)
7     return bar
8 a=foo()
9 a()
运行结果:
fangjiyi

二.匿名函数

1.定义:
lambda x:x+1
形参 返回值(包含运算逻辑)
2.使用方式
赋函数名实现
单个参数
name=‘fang‘
func=lambda x:x+‘sb‘
func(name)
print(func(name))
多个参数:
f=lambda x,y,z:(x+1,y+1,z+1)
print(f(5,6,7))
打印结果:
(6,7,8)

三.函数式编程
函数式编程=编程语言定义的函数+数学意义的函数
1.高阶函数
定义(满足以下其一)
*1函数接收的参数是一个函数名 *2返回值中包含函数
*把函数当作参数传给另外一个函数
def foo(n):
print(n)

def bar(name):
print(‘my name is %s‘ %name)
foo(bar)
foo(bar(‘alex‘))
*返回值中包含函数
方式一
def bar():
print(‘from bar‘)
def foo():
print(‘from foo‘)
return bar
n=foo()
n()
方式二
def hanle():
print(‘from handle‘)
return hanle
h=hanle()
h()
运行结果:
from handle
from handle
2.高阶函数--filter

filter()函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。

需求:过滤掉带前缀的字符串
1.使用正常方式(类似filter实现的过程)
students=[‘sb-bcs‘,‘sb-ljf‘,‘fjy-nb‘,‘ql-nb‘,‘zsb‘]

def sb_reduce(func,array):
ret=[]
for i in array:
if not func(i):
ret.append(i)
return ret

print(sb_reduce(lambda x:x.startswith(‘sb‘),students) )
#运行结果:
[‘fjy‘, ‘ql‘, ‘zsb‘]
2.使用filter函数
*filter返回的是迭代器对象
students=[‘sb-bcs‘,‘sb-ljf‘,‘fjy-nb‘,‘ql-nb‘,‘zsb‘]
print(filter(lambda x:x.startswith(‘sb‘),students) )
打印结果:
<filter object at 0x000001C1CFEF4978>
**通过list方法遍历取值(默认取判断正确的)
students=[‘sb-bcs‘,‘sb-ljf‘,‘fjy-nb‘,‘ql-nb‘,‘zsb‘]
print(list(filter(lambda x:x.startswith(‘sb‘),students)) )
打印结果:
[‘sb-bcs‘, ‘sb-ljf‘]
***进行 判断修改 实现过滤
students=[‘sb-bcs‘,‘sb-ljf‘,‘fjy-nb‘,‘ql-nb‘,‘zsb‘]
print(list(filter(lambda x:not x.startswith(‘sb‘),students)) )
打印结果:
[‘fjy-nb‘, ‘ql-nb‘, ‘zsb‘]

3.高阶函数--map
map()函数会根据提供的函数对指定序列做映射
需求:对任意数组实现加减乘除等处理
*正常实现(同时也是map函数实现过程)
 1 num_l=[1,2,10,5,3,7]
 2 def map_test(func,array):
 3     ret=[]
 4     for i in array:
 5         res=func(i) #add_one(i)
 6         ret.append(res)
 7     return ret
 8 
 9 print(map_test(lambda x:x+1,num_l))
10 print(map_test(lambda x:x-1,num_l))
11 print(map_test(lambda x:x*2,num_l))
12 print(map_test(lambda x:x/2,num_l))

打印结果:
[2, 3, 11, 6, 4, 8]
[0, 1, 9, 4, 2, 6]
[2, 4, 20, 10, 6, 14]
[0.5, 1.0, 5.0, 2.5, 1.5, 3.5]
**map函数
map函数返回的是一种可迭代对象
num_l=[1,2,10,5,3,7]
print(map(lambda x:x+1,num_l))
打印结果:
<map object at 0x000001FE068CA048>
***使用map函数需要用list取值出来
num_l=[1,2,10,5,3,7]
print(list(map(lambda x:x+1,num_l)))
打印结果;
[2, 3, 11, 6, 4, 8]

4.高阶函数--reduce()
直接返回一个具体的值
reduce() 函数会对参数序列中元素进行累积。
需求:对一个数组进行累积
*正常实现(reduce实现原理)
 1 num_l=[2,2,3,10]
 2 def reduce_test(func,array,init=None):
 3     if init is None:
 4         res=array.pop(0)
 5     else:
 6         res=init
 7     for num in array:
 8         res=func(res,num)
 9     return res
10 print(reduce_test(lambda x,y:x*y,num_l)))
11 print(reduce_test(lambda x,y:x*y,num_l,10))#指定参数从他的基础上进行
打印结果:
1200
60000
**使用reduce函数
需要从functols模块中引入
from functools import reduce
num_l=[3,2,2,9]
print(reduce(lambda x,y:x+y,num_l,1))
print(reduce(lambda x,y:x+y,num_l))

运行结果:
17
16

新手可能有很多不足

函数(作用域,匿名函数,函数式编程,高阶函数)

标签:color   pop   包含   声明   col   函数名   函数式编程   内存地址   迭代器   

原文地址:https://www.cnblogs.com/snowony/p/11779768.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!