函数进阶

简介: 作用域 print('in the test1') def test(): print('in the test') return test1 test() ---- in the test in the test in the test.

函数的作用域

def test1():
    print('in the test1')
def test():
    print('in the test')
    return test1
    
test()
----
in the test
in the test
in the test1

嵌套函数的执行方法

def foo():
    name='lhf'
    def bar():
        name='wupeiqi'
        def tt():
            print(name)
        return tt
    return bar

foo()()()
---
wupeiqi

匿名函数

lambda x:x+1
print(lambda x:x+1)
func=lambda x:x+1
print(func(10))
---
<function <lambda> at 0x100662e18>
11

name='alex'
func=lambda x:x+'_sb'
res=func(name)
print('匿名函数的运行结果',res)
---
匿名函数的运行结果 alex_sb

匿名函数传多个参数

lambda x,y,z:(x+1,y+1,z+1)
f=lambda x,y,z:(x+1,y+1,z+1)
v=f(1,2,3)
print(v)
---
(2,3,4)

函数式编程

高阶函数

满足俩个特性任意一个即为高阶函数
1.函数的传入参数是一个函数名
2.函数的返回值是一个函数名

什么是函数式编程

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

例一:不可变:不用变量保存状态,不修改变量
# 非函数式
a=1
def incr_test1():
    global a
    a+=1
    return a
incr_test1()
print(a)

# 函数式
n = 1
def incr_test2(n):
    return n+1
print(incr_test2(2))
print(n)

例二:第一类对象:函数即“变量”

1.函数名可以当做参数传递

  1. 返回值可以是函数名

例三:

尾调用: 在函数的最后一步调用另外一个函数(最后一行不一定是函数的最后一步)

def foo(n):
    print(n)

def bar(name):
    print('my name is %s' %name)

foo(bar('alex'))
---
my name is alex
None

返回值中包含函数。

def bar():
    print('from bar')
def foo():
    print('from foo')
    return bar
n=foo()
n()

map函数

num_l=[1,2,5,10]

def add_one(x):
    return x+1

def reduce_one(x):
    return x-1

def map_test(func,array):
    ret=[]
    for i in num_l:
        res=func(i)
        ret.append(res)

    return ret

print(map_test(add_one,num_l))
print()

终极版

def map_test(func,array):
    ret=[]
    for i in num_l:
        res=func(i)
        ret.append(res)
    return ret
map_test(lambda x:x+1,num_l)

内置函数

map

依次处理列表中的每一个元素,得到的结果还是一个列表
第一个参数为函数,第二个参数为可迭代对象

print(map_test(lambda x:x+1,num_l))
res=map(lambda x:x+1,num_l)
print('内置函数map,处理结果',res)
print(list(res))
print(传的是有名的函数',list(map(reduce_one,num_l))

filter

把列表中的值都删选一遍,最后得一个列表出来

# 面向过程的写法
movie_people=['alex_sb','wupeiqi_sb','linhaifeng','yuanhao_sb']
def filter_test(func,array):
    ret=[]
    for p in array:
        if not func(p):
            ret.append(p)
    return ret
res=filter_test(lambda n:n.endswith('sb'),movie_people)
print(res)

reduce函数

把一个完整的序列最后合并到一块得到一个值。

# 
num_l=[1,2,3,100]
def reduce_test(func,array,init=None):
    if init is None:
        res=array.pop(0)
    else:
        res=init
    for num in array:
        res=func(res,num)
    return res
print(reduce_test(lambda x,y:x*y,num_l,100))

# 用reduce来实现的方法
from functools import reduce
num_l=[1,2,3,100]
print(reduce(lambda x,y:x+y,num_l,1))
print(reduce(lambda x,y:x+y,num_l))

内置函数总结

  • map(): 处理序列中的每个元素,得到的结果是一个列表,该列表元素个数及位置与原来一样
  • filter(): 遍历序列中的每个元素,判断每个元素得到bool值,如果是True则留下来。
people=[
    {'name':'alex','age':1000},
    {'name':'yuanhao','age':10000},
    {'name':'wupeiqi','age':9000},
    {'name':'linhaifeng','age':18},
]
print(list(filter(lambda p:p['age']<=18,people)))
---
[{'name': 'linhaifeng', 'age': 18}]
  • reduce(): 处理一个序列,然后把序列进行合并操作
目录
相关文章
|
4月前
|
JavaScript 编译器 API
【C++ 函数和过程 进阶篇】全面掌握C++函数返回值:从入门到精通的实战指南
【C++ 函数和过程 进阶篇】全面掌握C++函数返回值:从入门到精通的实战指南
154 1
|
11月前
|
存储
【C进阶】回调函数(指针进阶2,详解,小白必看)(上)
【C进阶】回调函数(指针进阶2,详解,小白必看)(上)
|
11月前
【C进阶】回调函数(指针进阶2,详解,小白必看)(中)
【C进阶】回调函数(指针进阶2,详解,小白必看)(中)
|
4月前
|
C++ 编译器 程序员
C++ 从零基础到入门(3)—— 函数基础知识
C++ 从零基础到入门(3)—— 函数基础知识
基础知识 函数
基础知识 函数
60 0
|
4月前
|
人工智能 C++
C/C++基础知识——函数
C/C++基础知识——函数
53 0
|
11月前
|
索引
【C进阶】回调函数(指针进阶2,详解,小白必看)(下)
【C进阶】回调函数(指针进阶2,详解,小白必看)(下)
|
算法 程序员 编译器
『C语言从入门到进阶』第 ③ 期 - 函数
『C语言从入门到进阶』第 ③ 期 - 函数
82 0
|
C语言 Python
函数基础知识以及特殊点
函数基础知识以及特殊点
123 0
函数基础知识以及特殊点