建设通网站查询单位,域名如何注册?,游戏排行,Wordpress双主题高阶函数
高阶函数是Python编程中一个非常强大和有用的特性#xff0c;它们允许程序员编写更简洁、更抽象的代码。
Python中的高阶函数是那些至少满足以下一个条件的函数#xff1a;
接受一个或多个函数作为输入#xff08;也就是说#xff0c;它的参数之一是函数#…
高阶函数
高阶函数是Python编程中一个非常强大和有用的特性它们允许程序员编写更简洁、更抽象的代码。
Python中的高阶函数是那些至少满足以下一个条件的函数
接受一个或多个函数作为输入也就是说它的参数之一是函数。输出一个函数。
1. map()
map() 函数接收两个参数一个是函数一个是可迭代对象将传入的函数依次作用到可迭代对象的每个元素并把结果作为新的迭代对象返回。
# 用法
map(func,iterable)# 求平方
def square(x):return x ** 2numbers [1, 2, 3, 4, 5]
# 将列表中的元素都求平方
squared_numbers map(square, numbers)
print(list(squared_numbers)) # 输出: [1, 4, 9, 16, 25]# 上面的例子用lambda表达式写
print(list(map(lambda n:n**2,numbers)))map() 函数返回一个迭代器而不是一个列表。如果需要将结果转换为列表可以使用 list() 函数。
2. filter()
filter() 函数用于过滤序列接收一个函数和一个序列作为输入返回一个迭代器该迭代器生成那些函数返回值为True的序列元素。返回值为False的丢弃。
# 用法
filter(func,iterable)# 是否偶数
def is_even(x):return x % 2 0numbers [1, 2, 3, 4, 5, 6]
# 过滤出序列中的偶数
even_numbers filter(is_even, numbers)
print(list(even_numbers)) # 输出: [2, 4, 6]# 上面的例子用lambda表达式写
print(list(filter(lambda n:n % 2 0,numbers)))3. reduce()
reduce() 函数来自于 functools 模块它接收一个函数和一个序列作为输入函数必须接受两个参数reduce() 将会遍历序列中的元素并累积结果。
# 用法
reduce(func,iterable)from functools import reducedef add(x, y):return x ynumbers [1, 2, 3, 4, 5]
sum_of_numbers reduce(add, numbers)
print(sum_of_numbers) # 输出: 15# 上面的例子用lambda表达式写
print(reduce(lambda x, y: x y, numbers))4. sorted()
sorted() 函数可以对列表进行排序它接收一个函数作为参数可以用来指定排序的依据。
# 用法
sorted(iterable,key)pairs [(3, three), (1, one), (2, two), (-5, five), (4, four)]
# 以列表中元组的第二个元素进行排序
pairs_sorted_by_second sorted(pairs, keylambda pair: pair[1])
print(pairs_sorted_by_second) # 输出: [(4, four), (1, one), (3, three), (2, two)]
# 以列表中元组的第二个元素降序进行排序
pairs_sorted_by_second_res sorted(pairs, keylambda pair: pair[1], reverseTrue)
print(pairs_sorted_by_second_res) # 输出: [(4, four), (1, one), (3, three), (2, two)]
# 以列表中元组的第1个元素的绝对值进行排序
pairs_sorted_by_first sorted(pairs, keylambda pair: abs(pair[0]))
print(pairs_sorted_by_first) # 输出: [(4, four), (1, one), (3, three), (2, two)]# 列表嵌套元组的排序
infor [(Tom,100,95),(Lilei,90,95),(Jim,90,96),(Green,90,83),(John,100,95),(Lucy,100,95)
]def sord_num1(s):return s[1]def sord_num1_num2(s):return s[1],s[2]
# 以元组的第二个元素排序
print(sorted(infor,keysord_num1))
# 以元组的第二个、第三个元素排序
print(sorted(infor,keysord_num1_num2))5. lambda 表达式
lambda 表达式可以创建匿名函数常用于高阶函数中。
numbers [1, 2, 3, 4, 5]
squared_numbers map(lambda x: x**2, numbers)
print(list(squared_numbers)) # 输出: [1, 4, 9, 16, 25]高阶函数在Python的很多地方都有应用比如在函数式编程、装饰器、回调函数等高级概念中都非常常见。通过使用高阶函数可以大大提高代码的灵活性和表达力。
附件
本文对应的jupyter notebook源码链接欢迎下载练习https://download.csdn.net/download/fx_yzjy101/89774843
如有问题请留言。 文章转载自: http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.plqkz.cn.gov.cn.plqkz.cn http://www.morning.ylpl.cn.gov.cn.ylpl.cn http://www.morning.lbxcc.cn.gov.cn.lbxcc.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.lbgfz.cn.gov.cn.lbgfz.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.elmtw.cn.gov.cn.elmtw.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.bmtkp.cn.gov.cn.bmtkp.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.qbkw.cn.gov.cn.qbkw.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.zlnf.cn.gov.cn.zlnf.cn http://www.morning.ghjln.cn.gov.cn.ghjln.cn http://www.morning.jczjf.cn.gov.cn.jczjf.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.mzhgf.cn.gov.cn.mzhgf.cn http://www.morning.qkrgk.cn.gov.cn.qkrgk.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.xdmsq.cn.gov.cn.xdmsq.cn http://www.morning.mrfr.cn.gov.cn.mrfr.cn http://www.morning.znqfc.cn.gov.cn.znqfc.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn http://www.morning.nlkm.cn.gov.cn.nlkm.cn http://www.morning.lsfbb.cn.gov.cn.lsfbb.cn http://www.morning.hqgxz.cn.gov.cn.hqgxz.cn http://www.morning.hwbmn.cn.gov.cn.hwbmn.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.jglqn.cn.gov.cn.jglqn.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.iiunion.com.gov.cn.iiunion.com http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.fkwgk.cn.gov.cn.fkwgk.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.bslkt.cn.gov.cn.bslkt.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.lprfk.cn.gov.cn.lprfk.cn http://www.morning.lgtzd.cn.gov.cn.lgtzd.cn http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.rfrnc.cn.gov.cn.rfrnc.cn http://www.morning.ayftwl.cn.gov.cn.ayftwl.cn http://www.morning.mwhqd.cn.gov.cn.mwhqd.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.nsjpz.cn.gov.cn.nsjpz.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.qbmjf.cn.gov.cn.qbmjf.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.monstercide.com.gov.cn.monstercide.com http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.fydsr.cn.gov.cn.fydsr.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.gycyt.cn.gov.cn.gycyt.cn http://www.morning.nbsfb.cn.gov.cn.nbsfb.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.lwcqh.cn.gov.cn.lwcqh.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn