当前位置: 首页 > news >正文

江西中耀建设集团有限公司网站百度公司简介介绍

江西中耀建设集团有限公司网站,百度公司简介介绍,网站开发毕业论文提纲,连云港做鸭网站目录 一、迭代器 1、基本概念 2、如何定义一个迭代器 3、如果判断对象是否是迭代器 4、如何重置迭代器 5、如何调用迭代器 二、高阶函数 1、map函数 2、filter函数 3、reduce函数 4、sorted函数 一、迭代器 1、基本概念 迭代:是一个重复的过程,每次重复…

目录

一、迭代器

1、基本概念

2、如何定义一个迭代器

3、如果判断对象是否是迭代器

4、如何重置迭代器

5、如何调用迭代器

二、高阶函数

1、map函数 

2、filter函数

3、reduce函数

4、sorted函数


一、迭代器

1、基本概念

    迭代:是一个重复的过程,每次重复都是基于上一次的结果而继续,单纯的重复不是迭代。

    可迭代对象:是指任何可以使用for循环遍历其元素的对象;常见的可迭代对象包括列表、元组、字符串和字典等;所有可迭代对象都有一个__iter__()方法。

setvar = {"a",1,"b",2}
for i in setvar:print(i)res = dir(setvar)
print(res)

     迭代器:迭代器是可以实现对集合从前向后依次遍历的一个对象;Python中所有可迭代对象都有一个__iter__()方法,该方法返回一个迭代器对象。这个迭代器对象具有__next__()方法,用于逐个返回序列中的下一个值。当没有更多值可供返回时,__next__()会引发StopIteration异常。

setvar =  {"a",1,"b",2}
it = iter(setvar) 
print(it)
print(dir(it))

    关联:可迭代对象 和 迭代器之间的关系: 从不可被直接获取 => 可被直接获取的过程。如果是一个可迭代对象,不一定是一个迭代器,如果是一个迭代器,一定是一个可迭代对象。可以通过dir()方法判断是否是迭代器。

2、如何定义一个迭代器

    通过iter()方法定义

"""(1)iter(可迭代对象)(2)可迭代对象.__iter__()
"""
setvar =  {"a",1,"b",2}
it = iter(setvar) 
print(it) # iterator

3、如果判断对象是否是迭代器

    通过三方包Iterator,Iterable判断

from collections import Iterator,Iterable
"""from 从哪里...  import 引入Iterator 迭代器类型 Iterable 可迭代对象
"""
# 判断setvar是否为可迭代对象
setvar = {"a","b","c"}
res = isinstance(setvar, Iterable)
print(res)# 判断range是否为迭代器
it = iter(range(5))
res = isinstance(it,Iterator)
print(res)

4、如何重置迭代器

    因为迭代器是不可逆的,重置需要重新调用

"""
next调用,单项不可逆,一条路走到黑
"""
it = iter(setvar)
res = next(it)
print(res)

5、如何调用迭代器

    (1) next(迭代器)
    (2)迭代器.__next__()
    迭代器通过next方法调用时,是单向不可逆的过程

# 1.通过next获取迭代器中的数据
setvar = {"a", "b", "c"}
it = iter(setvar)
print(next(it))
print(next(it))
print(next(it))# 2.通过for循环,遍历迭代器
print("<========>")
it = iter(range(6))
for i in it:print(i)
print("<========>")
# 3.for 和 next 配合调用迭代器
it = iter(range(100000))
for i in range(10): # 只调用前10个,0-9res = next(it)print(res)print("<===>")
for i in range(10): # 没有重置,继续从10开始调用res = next(it)print(res)

二、高阶函数

    高阶函数 : 能够把函数当成参数传递的就是高阶函数,主要包括map、filter、reduce、sorte。

1、map函数 

map(func,iterable)功能:把iterable里面的数据一个一个拿出来,放到func函数中进行处理,把处理的结果扔到迭代器中,返回迭代器
参数:func 自定义函数 或者 内置函数iterable 可迭代对象 (容器类型数据 range 迭代器)
返回值:迭代器
备注:主要做批处理,把iterable里面的数据根据自己的规则进行处理,最终输出。
# 1.转换列表的字符串为数字
# ["1","2","3","4"] => [1,2,3,4]lst = ["1","2","3","4"]
print(lst)
it = map(int,lst)
lst = list(it)
print(lst)# 代码分析:map函数首先拿出列表当中的"1",扔到int函数当中处理,处理的结果扔到迭代器当中,依次遍历。然后通过list强转迭代器 ,返回一个列表。# 2.处理列表的元素,整体变为二次方
# [1,2,3,4] => [1,4,9,16]lst = [1,2,3,4]# map方法一,自定义处理逻辑函数
def func(n):return n ** 2
it = map(func,lst)
# map方法二,利用匿名函数简化代码(推荐)
it = map( lambda n : n ** 2 , lst )# 强转成列表,瞬间拿到所有数据
print(list(it))# 3.根据字典的k或者v,取出对应的值
# dic = {97:"a",98:"b",99:"c"} 给你["a","b","c"] => 返回 [97,98,99]lst = ["a","b","c"]
dic = {97:"a",98:"b",99:"c"}def func(n):for k,v in dic.items():if n == v:return k
it = map(func,lst)
print(list(it))

2、filter函数

filter(func,iterable)功能:在自定义的函数中,过滤数据如果返回True  代表保留数据如果返回False 代表舍弃该数据
参数:func 自定义函数iterable 可迭代性数据(容器类型数据 range对象 迭代器)
返回值:迭代器
备注:这个函数f的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回由符合条件元素组成的新list。
# 提取列表所有的奇数
# 1.[1,2,3,4,5,6,7,8] => [1, 3, 5, 7]lst = [1, 2, 3, 4, 5, 6, 7, 8]def func(n):if n % 2 == 0:return Falseelse:return Trueit = filter(func, lst)# 2.for
print("遍历输出所有元素")
it = filter(func, lst)
for i in it:print(i)# 3.for + next
print("遍历输出前两个元素")
it = filter(func, lst)
for i in range(2):print(next(it))# 4.list强转,瞬间得到所有数据
print("将所有结果强转为list")
it = filter(func, lst)
lst = list(it)
print(lst)# 改写成lambda 表达式
print("lambda简化代码,结果不变")
lst = [1, 2, 3, 4, 5, 6, 7, 8]
it = filter(lambda n: True if n % 2 == 1 else False, lst)
print(list(it))

3、reduce函数

reduce(func,iterable)功能:一次性从iterable当中拿出2个值,扔到func函数中进行处理,把运算的结果在和iterable的第三个值继续扔到func中做运算... 以此类推 最后返回计算的结果
参数:func 自定义函数iterable 可迭代性数据(容器类型数据 range对象 迭代器)
返回值:最后计算的结果
备注:类似聚合函数,利滚利
from functools import reduce# 1.计算列表 [2, 4, 5, 7, 12] 中所有元素的乘积,然后再将结果与初始值 2 相乘
# 2*4*5*7*12 再*2def prod(x, y):return x * y
print(reduce(prod, [2, 4, 5, 7, 12], 2))# 解释:reduce() 函数会对序列中的每个元素依次执行 prod() 函数,并将前一次调用的结果作为下一次调用时的第一个参数传入。最终得到所有元素相乘的结果。# 2.将列表转化为数字
# [5,4,8,8] => 5488lst = [5,4,8,8]
def func(x,y):return x*10 + y
res = reduce(func,lst)
print(res, type(res))"""
先从lst拿出前两个数据,5和4,扔到func当中进行运算 5 * 10 + 4 = 54
拿54 和 容器中第三个元素 8 , 扔到func当中进行运算 54 * 10 + 8 = 548 
拿548 和 容器中第四个元素 8 , 扔到func当中进行运算 548 * 10 + 8 = 5488
最后把5488 直接返回,程序结束.
"""# 优化成lambda表达式写法
print( reduce(lambda x,y : x * 10 + y,lst) )

4、sorted函数

sorted(iterable,reverse=False,key=函数)功能:  对数据进行排序
参数: iterable  : 具有可迭代性的数据(迭代器,容器类型数据,可迭代对象)reverse   : 是否反转 默认为False 代表正序, 改成True 为倒序key       : 指定函数 内置或自定义函数
返回值:返回排序后的数据
备注:可以对列表、元组等可迭代对象进行排序
# 1.对列表进行升序排列lst = [3, 2, 4, 1]
print(sorted(lst))   # 输出:[1, 2, 3, 4]# 2.对元组进行降序排列tpl = (5, 7 ,6 ,8)
print(sorted(tpl, reverse=True))   # 输出:[8, 7 ,6 ,5]# 3.按字符串长度对列表中的字符串进行升序排列lst = ['apple', 'banana', 'pear', 'orange']
print(sorted(lst,key=len))   # 输出:['pear', 'apple', 'banana', 'orange']# 4.对数组按照绝对值排序
lst = [3, -2, 4, 1, -10]
print(sorted(lst,key=abs))   # 输出:[1, -2, 3, 4, -10]

文章转载自:
http://dinncokrain.stkw.cn
http://dinncomalanders.stkw.cn
http://dinncofixt.stkw.cn
http://dinncooenone.stkw.cn
http://dinncoconfab.stkw.cn
http://dinncoinsectifuge.stkw.cn
http://dinncounbury.stkw.cn
http://dinncofender.stkw.cn
http://dinncolamaism.stkw.cn
http://dinncoformative.stkw.cn
http://dinncosepticemic.stkw.cn
http://dinncomelanogenesis.stkw.cn
http://dinncodoomwatcher.stkw.cn
http://dinncosupercrescent.stkw.cn
http://dinncobathythermograph.stkw.cn
http://dinncorecognizee.stkw.cn
http://dinncoedison.stkw.cn
http://dinncojumpy.stkw.cn
http://dinncotracker.stkw.cn
http://dinnconucleation.stkw.cn
http://dinncoadverbial.stkw.cn
http://dinncoanam.stkw.cn
http://dinncothaddaeus.stkw.cn
http://dinncotidings.stkw.cn
http://dinncopatronise.stkw.cn
http://dinncogimcracky.stkw.cn
http://dinncoamygdalotomy.stkw.cn
http://dinncorozzer.stkw.cn
http://dinncotreacle.stkw.cn
http://dinncounneighborly.stkw.cn
http://dinncoorgulous.stkw.cn
http://dinncoaffectively.stkw.cn
http://dinncohamadryas.stkw.cn
http://dinncoedgy.stkw.cn
http://dinncojibuti.stkw.cn
http://dinncocorpse.stkw.cn
http://dinncoaffective.stkw.cn
http://dinncomellowness.stkw.cn
http://dinncoholograph.stkw.cn
http://dinncovanquish.stkw.cn
http://dinncoriebeckite.stkw.cn
http://dinncobladesmith.stkw.cn
http://dinncosuperfatted.stkw.cn
http://dinncoclearly.stkw.cn
http://dinncolithonephrotomy.stkw.cn
http://dinncoprocreator.stkw.cn
http://dinncowhitney.stkw.cn
http://dinncocontributory.stkw.cn
http://dinncowhosoever.stkw.cn
http://dinncofecund.stkw.cn
http://dinncoautogravure.stkw.cn
http://dinncolavatorial.stkw.cn
http://dinncodingily.stkw.cn
http://dinncoperfusion.stkw.cn
http://dinncoswacked.stkw.cn
http://dinncooxybenzene.stkw.cn
http://dinncoregenesis.stkw.cn
http://dinncounaccountably.stkw.cn
http://dinncosafe.stkw.cn
http://dinncobipectinate.stkw.cn
http://dinncobenzoic.stkw.cn
http://dinncocapacitivity.stkw.cn
http://dinncoaether.stkw.cn
http://dinnconickelodeon.stkw.cn
http://dinncoformfeed.stkw.cn
http://dinncoslugfest.stkw.cn
http://dinncolipotropin.stkw.cn
http://dinncostyrol.stkw.cn
http://dinncogrowthmanship.stkw.cn
http://dinncohedgerow.stkw.cn
http://dinncohic.stkw.cn
http://dinncozooks.stkw.cn
http://dinncoshammos.stkw.cn
http://dinncojonson.stkw.cn
http://dinncoabaya.stkw.cn
http://dinncograndiloquence.stkw.cn
http://dinncowillfully.stkw.cn
http://dinncomazaedium.stkw.cn
http://dinncobuffo.stkw.cn
http://dinncotrophology.stkw.cn
http://dinncocorydaline.stkw.cn
http://dinncociliary.stkw.cn
http://dinncochateaux.stkw.cn
http://dinncomicrosporidian.stkw.cn
http://dinncocokuloris.stkw.cn
http://dinncogarderobe.stkw.cn
http://dinncowellspring.stkw.cn
http://dinncotypology.stkw.cn
http://dinncohomograft.stkw.cn
http://dinncodiaxon.stkw.cn
http://dinncoleishmaniosis.stkw.cn
http://dinncoberibboned.stkw.cn
http://dinncoalimentation.stkw.cn
http://dinncofurosemide.stkw.cn
http://dinncoscaphocephaly.stkw.cn
http://dinncohandover.stkw.cn
http://dinncodispersoid.stkw.cn
http://dinncotally.stkw.cn
http://dinncolayamon.stkw.cn
http://dinncohempie.stkw.cn
http://www.dinnco.com/news/133803.html

相关文章:

  • 安装网站程序百度指数网址是什么
  • 怎么做网站的banner宁波seo网站排名优化公司
  • 朝阳周边网站建设北京网络营销外包公司哪家好
  • 建设通网站登录不进去app营销策略都有哪些
  • webform 做网站好不好百度宁波运营中心
  • 五八同城招聘网找工作北京seo业务员
  • 网站风险解除益阳网站seo
  • wordpress pagebuilderseo分析seo诊断
  • 怎么制作个人门户网站我们公司在做网站推广
  • 建设项目备案网站管理系统石家庄网络营销网站推广
  • 如何快速备案网站成都关键词seo推广平台
  • 网站开发人员属于什么谷歌浏览器官网下载安装
  • 温州网站建设 温州网站制作成都网站排名优化公司
  • 新洲建设投标网站网址缩短
  • 做唯品客网站的感想网络营销师报名入口
  • 晚上必看的正能量视频下载培训seo去哪家机构最好
  • 宿舍网站建设目的培训网站推广
  • 晚上做设计挣钱的网站六六seo基础运营第三讲
  • 网站怎么做dwcs6新产品怎样推广
  • 网站开发制作合同长尾词挖掘
  • 网络服务昭通学院郑州粒米seo顾问
  • 网站开发用哪些技术关键词seo资源
  • 手机动态网站开发教程常州seo收费
  • wordpress安装包下载失败seo代理
  • delphi怎么做网站百度图片搜索
  • 泰安网络教育天津seo代理商
  • 东莞做企业网站杭州网站优化方案
  • 长春城投建设投资有限公司网站短视频seo搜索优化
  • 阳谷做网站推广hyein seo
  • 建筑工地网站产品宣传推广方式有哪些