递归函数
在一个函数体内部,调用函数本身
def func(n):
if n ==1:
return (1)
elif n ==2:
return (1)
else:
return func(n-2)+func(n-1)
print(func(10))
def func(n):
print(n)
if int(n/2)==0:
return n
return func(int(n/2))
print(func(10))
匿名函数(lambda)
- 格式:
lambda para1, para2,..., paraN:expression using paras
f = lambda x, y, z: x+y+z
print(type(f))
print(f(1, 2, 3))
<class 'function'>
6
f = lambda x = 'z', y = 'u',z = 'cc':x+y+z
print(f())
zucc
高阶函数
把一个函数名,以实参的形式,传递给这个函数的形参
def add(a, b, c):
return c(a) + c(b)
print(add(-9, 1, abs))
10
def pow(x):
return x**2
def add(a, b, func):
return func(a) + func(b)
a_value = add(-9, 1, pow)
print(a_value)
82
li = ['Zhejiang', 'University', 'City', 'College']
def filter_test1(para):
ret = []
for i in para:
if not i.startswith('C'):
ret.append(i)
return ret
def filter_test2(para):
ret = []
for i in para:
if not i.endswith('ty'):
ret.append(i)
return ret
def test(a,func,func1):
return func1(func(a))
zzz = test(li, filter_test1,filter_test2)
print(zzz)
['Zhejiang']
filter 函数
li = ['Zhejiang', 'University', 'City', 'College']
f1 = filter(lambda sr: not sr.endswith('ty'),li)
print(list(f1))
['Zhejiang', 'College']
import random
def odd(n):
return n%2
allnum = []
for i in range(9):
allnum.append(random.randint(1,99))
f1 = filter(odd, allnum)
print(list(f1))
[55, 41, 67, 47]
功能:
-
过滤保存序列中符合函数条件的元素。当序列中要需要保留的元素可以用某些函数描述时,就应该想到filter函数 filter返回值是一个地址
-
filter(function,sequence)- function —> 可以是自定义的函数,也可匿名函数
- sequence —> 列表,元组,字符串
map 映射
功能
- 求一个序列或者多个序列进行函数映射后的值。(用list()强转)
格式
map(function, iterable1, iterable2)- function 的参数可以不止有一个
- iterable1, iterable2 就是传入fuction的参数
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]
def f(x, y):
return x*y + 2
res = map(f, x, y)
print(list(res))
[4, 8, 14, 22, 32]
reduce 函数
- 功能:
- 对一个序列进行压缩运算,得到一个value
- python2中,reduce()是内置函数, 现在py3中,移植到了functools模块中
from functoolsimport reduce`
- 格式
- reduce(function, iterable, [initial])
- function必须传入两个参数
- iterabel —> 列表/元组
- reduce(function, iterable, [initial])
from functools import reduce
y = [2, 3, 4, 5, 6]
z = reduce(lambda x, y: x+y, y)
print(z)
#20 2+3 +4 +5 +6
y = [2, 3, 4, 5, 6]
z = reduce(lambda x, y: x-y, y, 100) # 传入初始值,insert列表index 0 位置即 [100, 2, 3, 4, 5, 6]
print(z)
#80 100-2 -3 -4 -5 -6
y = [2, 3, 4, 5, 6]
def sum(a, b):
return a+b # 10*a+b 结果就会是 23456
c = 0
for i in y:
c = sum(c, i)
print(c)
apply
功能
- pandas 中,应用对象是pandas中的DataFrame或者Series
- 直接对DataFrame或者Series应用函数
- 对pandas中groupby之后的聚合对象应用apply
zip
功能
- 将可迭代对象作为参数,将对应元素打包成 一个个元组,返回元组构成对象
- 长度不一样的时候,以短的为准
注:
利用 \*,与zip相反,进行解压
格式
zip(iterable1, iterable2, …)
- iterables —> 两个或者多个可迭代序列(字符串, 列表, 元组, 字典)
- py2,返回的是元组组成的列表
- Py3,返回的是一个对象,需要强转
a = [1, 2, 3]
b = [4, 5, 6]
c = [4, 5, 6, 7, 8, ]
zip1 = zip(a,b)
print(list(zip1))
#[(1, 4), (2, 5), (3, 6)]
a = [1, 2, 3]
b = [4, 5, 6]
c = [4, 5, 6, 7, 8, ]
zip1 = zip(a,b)
start_zip = zip(*zip1)
print(list(start_zip))
#[(1, 2, 3), (4, 5, 6)]
a = {1:11, 2:22}
b = {3:33, 4:44}
c = {5:55, 6:66}
tp = tuple(c)
print(tp)
print(list(zip(a, b, c)))
(5, 6)
[(1, 3, 5), (2, 4, 6)]
#对字典操作 只选择键
小练习
def out(n):
return str(n) == str(n)[::-1]
allnum = []
for i in range(1000):
allnum.append(i)
f1 = filter(out, allnum)
print(list(f1))
f2 = filter(out, range(1000))
print(list(f2))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, 212, 222, 232, 242, 252, 262, 272, 282, 292, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 404, 414, 424, 434, 444, 454, 464, 474, 484, 494, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 606, 616, 626, 636, 646, 656, 666, 676, 686, 696, 707, 717, 727, 737, 747, 757, 767, 777, 787, 797, 808, 818, 828, 838, 848, 858, 868, 878, 888, 898, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]
判断 回文数
本文深入探讨了递归函数的实现原理,包括匿名函数(lambda)和高阶函数的应用,通过具体示例展示了如何使用filter、map、reduce等函数进行数据处理。同时,文章还介绍了Python中重要的函数式编程概念。
1002

被折叠的 条评论
为什么被折叠?



