函数补充:递归、匿名、高阶函数等

递归函数

在一个函数体内部,调用函数本身

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 functools import reduce`
  • 格式
    • reduce(function, iterable, [initial])
      • function必须传入两个参数
      • iterabel —> 列表/元组
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]
判断 回文数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容介绍 项目结构: Controller层:使用Spring MVC来处理用户请求,负责将请求分发到相应的业务逻辑层,并将数据传递给视图层进行展示。Controller层通常包含控制器类,这些类通过注解如@Controller、@RequestMapping等标记,负责处理HTTP请求并返回响应。 Service层:Spring的核心部分,用于处理业务逻辑。Service层通过接口和实现类的方式,将业务逻辑与具体的实现细节分离。常见的注解有@Service和@Transactional,后者用于管理事务。 DAO层:使用MyBatis来实现数据持久化,DAO层与数据库直接交互,执行CRUD操作。MyBatis通过XML映射文件或注解的方式,将SQL语句与Java对象绑定,实现高效的数据访问。 Spring整合: Spring核心配置:包括Spring的IOC容器配置,管理Service和DAO层的Bean。配置文件通常包括applicationContext.xml或采用Java配置类。 事务管理:通过Spring的声明式事务管理,简化了事务的处理,确保数据一致性和完整性。 Spring MVC整合: 视图解析器:配置Spring MVC的视图解析器,将逻辑视图名解析为具体的JSP或其他类型的视图。 拦截器:通过配置Spring MVC的拦截器,处理请求的预处理和后处理,常用于权限验证、日志记录等功能。 MyBatis整合: 数据源配置:配置数据库连接池(如Druid或C3P0),确保应用可以高效地访问数据库。 SQL映射文件:使用MyBatis的XML文件或注解配置,将SQL语句与Java对象映射,支持复杂的查询、插入、更新和删除操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值