网站要跟换域名怎么做,昆明网站建设推广优化,长沙企业网站建设哪家好,怎么做一个网站的logo设计图目录
#xff08;一#xff09;体验高阶函数
#xff08;二#xff09;内置高阶函数
2.1 map(#xff09;
2.2 reduce()
2.3 filter() Python中的高阶函数是指那些接受函数作为参数#xff0c;或者返回函数作为结果的函数。这种特性让Python的函数编程能力非常强大一体验高阶函数
二内置高阶函数
2.1 map(
2.2 reduce()
2.3 filter() Python中的高阶函数是指那些接受函数作为参数或者返回函数作为结果的函数。这种特性让Python的函数编程能力非常强大使得代码更加灵活和可重用。
一体验高阶函数
在Python中abs()函数可以完成对数字求绝对值计算。
abs(-10) #10
round()函数可以完成对数字的四舍五入计算。
round(1.2) #1round(1.9) #2 需求任意两个数字按照指定要求整理数字后再进行求和计算。 方法1
def add_num(a,b):return abs(a) abs(b)result add_num(-1,2)
print(result) #3
方法2
def sum_num(a,b,f):return f(a) f(b)result sum_num(-1,2,abs)
print(result) #3 注意两种方法对比之后发现方法2的代码会更加简洁函数灵活性更高。 函数式编程大量使用函数减少了代码的重复因此程序比较短开发速度较快。 二内置高阶函数
2.1 map(
描述
在Python中map 是一个内置的高阶函数它接收一个函数作为第一个参数通常称为“映射函数”然后接收一个或多个可迭代对象如列表、元组、集合等作为后续参数。
语法
map(function, iterable, ...)将传入的函数变量func作用到iterable变量的每个元素中并将结果组成新的列表(Python2)/迭代器(Python3)返回。 需求计算list1序列中各个数字的2次方。 list1 [1,2,3,4,5]
def func(x):return x ** 2result map(func,list1)print(result) #map object at 0x000002AFA5106AD0
print(list(result)) #[1,4,9,16,25]
【解释说明】
首先定义了一个列表 list1 和一个函数 func。
list1 [1,2,3,4,5]这是一个包含五个整数的列表。def func(x): return x ** 2这是一个函数它接受一个参数 x 并返回 x 的平方。
接下来使用 map() 函数和 func 函数来处理 list1 中的每一个元素。
result map(func, list1)map() 函数会遍历 list1 中的每一个元素并将它们作为参数传递给 func 函数。然后它会收集 func 函数的返回值并创建一个迭代器该迭代器包含所有这些返回值。在这个例子中map() 函数的返回值即 result是一个迭代器它包含 1 的平方、2 的平方、3 的平方、4 的平方和 5 的平方。
然后尝试打印 result
print(result)因为 result 是一个迭代器所以直接打印它会显示迭代器的内存地址如 map object at 0x000002AFA5106AD0而不是迭代器中的实际值。
最后使用 list() 函数将 result 迭代器转换为列表并打印结果
print(list(result))这里list() 函数会迭代 result 迭代器并收集所有的值到一个新的列表中。因此输出是 [1, 4, 9, 16, 25]这是 list1 中每个元素的平方。
需要注意的是一旦您使用 list(result) 将迭代器转换为列表result 迭代器就被“消耗”了。这意味着如果再次尝试将 result 转换为列表或进行其他迭代操作您将不会得到任何值因为迭代器已经到达了其末尾。 2.2 reduce()
描述
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数返回包含每次 function 函数返回值的新列表。
语法
reduce(func,lst)
其中func必须有两个参数。每次func计算的结果继续和序列的下一个元素做累积计算。注意reduce(0传入的参数func必须接收2个参数。 需求计算list1序列中各个数字的累加和。 import functoolslist1 [1,2,3,4,5]def func(a,b):return a bresult functools.reduce(func,list1)
print(result) 2.3 filter()
描述
filter函数用于过滤序列过滤掉不符合条件的元素返回一个filter对象。如果要转换为列表,可以使用list()来转换。
语法
filter(function, iterable)function -- 判断函数。iterable -- 可迭代对象。 示例
list1[1,2,3,4,5,6,7,8,9,10]
def func(x):return x%2 0result filter(func,list1)
print(result) #filter object at 0x0000013D68516B00
print(list(result)) #[2,4,6,8,10]
文章转载自: http://www.morning.fhrgk.cn.gov.cn.fhrgk.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.hxcrd.cn.gov.cn.hxcrd.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.rmfw.cn.gov.cn.rmfw.cn http://www.morning.bkryb.cn.gov.cn.bkryb.cn http://www.morning.rknjx.cn.gov.cn.rknjx.cn http://www.morning.yxplz.cn.gov.cn.yxplz.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.kkgbs.cn.gov.cn.kkgbs.cn http://www.morning.lrflh.cn.gov.cn.lrflh.cn http://www.morning.nqlx.cn.gov.cn.nqlx.cn http://www.morning.21r000.cn.gov.cn.21r000.cn http://www.morning.lywcd.cn.gov.cn.lywcd.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn http://www.morning.mwnch.cn.gov.cn.mwnch.cn http://www.morning.xcszl.cn.gov.cn.xcszl.cn http://www.morning.lsfrc.cn.gov.cn.lsfrc.cn http://www.morning.yqqgp.cn.gov.cn.yqqgp.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.xhklb.cn.gov.cn.xhklb.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.rxsgk.cn.gov.cn.rxsgk.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.kpcky.cn.gov.cn.kpcky.cn http://www.morning.kxmyj.cn.gov.cn.kxmyj.cn http://www.morning.qlkjh.cn.gov.cn.qlkjh.cn http://www.morning.cnfxr.cn.gov.cn.cnfxr.cn http://www.morning.gediba.com.gov.cn.gediba.com http://www.morning.mlfgx.cn.gov.cn.mlfgx.cn http://www.morning.hjsrl.cn.gov.cn.hjsrl.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.mdpcz.cn.gov.cn.mdpcz.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.jjzrh.cn.gov.cn.jjzrh.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.ctswj.cn.gov.cn.ctswj.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.srhqm.cn.gov.cn.srhqm.cn http://www.morning.wcghr.cn.gov.cn.wcghr.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.lgsqy.cn.gov.cn.lgsqy.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.rwqj.cn.gov.cn.rwqj.cn http://www.morning.xzgbj.cn.gov.cn.xzgbj.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.bfhfb.cn.gov.cn.bfhfb.cn http://www.morning.gktds.cn.gov.cn.gktds.cn http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn http://www.morning.bpmfq.cn.gov.cn.bpmfq.cn http://www.morning.ntkpc.cn.gov.cn.ntkpc.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.dppfh.cn.gov.cn.dppfh.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.pljxz.cn.gov.cn.pljxz.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.jmspy.cn.gov.cn.jmspy.cn