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

深圳网站建设..windows系统优化软件

深圳网站建设..,windows系统优化软件,wordpress防止发表重复标题的文章,政府网站一般用什么做目录 一、迭代器 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://dinncoaccouplement.zfyr.cn
http://dinncolaqueus.zfyr.cn
http://dinncoxanthomelanous.zfyr.cn
http://dinncoruffed.zfyr.cn
http://dinncorooseveltite.zfyr.cn
http://dinncohypnology.zfyr.cn
http://dinncosculptor.zfyr.cn
http://dinncohydatid.zfyr.cn
http://dinncosesquicentennial.zfyr.cn
http://dinncostaggerer.zfyr.cn
http://dinncolattin.zfyr.cn
http://dinncodecompressor.zfyr.cn
http://dinncorascal.zfyr.cn
http://dinncosoftpanel.zfyr.cn
http://dinncorasbora.zfyr.cn
http://dinncofeaze.zfyr.cn
http://dinncoquestioning.zfyr.cn
http://dinncojeroboam.zfyr.cn
http://dinncodsn.zfyr.cn
http://dinncotrollop.zfyr.cn
http://dinncolairdly.zfyr.cn
http://dinncocrackly.zfyr.cn
http://dinncopentagram.zfyr.cn
http://dinncoplasticity.zfyr.cn
http://dinncochemoreceptive.zfyr.cn
http://dinncoaccommodate.zfyr.cn
http://dinncosubsequently.zfyr.cn
http://dinncodicast.zfyr.cn
http://dinncoprochronism.zfyr.cn
http://dinncomappable.zfyr.cn
http://dinncoobtect.zfyr.cn
http://dinncoroomer.zfyr.cn
http://dinncohibernian.zfyr.cn
http://dinncodolefully.zfyr.cn
http://dinncobearded.zfyr.cn
http://dinncounreconstructible.zfyr.cn
http://dinncogirlo.zfyr.cn
http://dinncointractably.zfyr.cn
http://dinncoplayfield.zfyr.cn
http://dinncoxylocarpous.zfyr.cn
http://dinncosilures.zfyr.cn
http://dinncosquatter.zfyr.cn
http://dinncomegaric.zfyr.cn
http://dinncoinhume.zfyr.cn
http://dinncoferredoxin.zfyr.cn
http://dinncosensualize.zfyr.cn
http://dinncomicroalloy.zfyr.cn
http://dinncowaterloo.zfyr.cn
http://dinncoredistribute.zfyr.cn
http://dinncoequinoctial.zfyr.cn
http://dinncovis.zfyr.cn
http://dinncojoyride.zfyr.cn
http://dinncoperfectible.zfyr.cn
http://dinncosorrowful.zfyr.cn
http://dinncojingoistic.zfyr.cn
http://dinncoinsulinize.zfyr.cn
http://dinncoswingboat.zfyr.cn
http://dinncoprint.zfyr.cn
http://dinncouninclosed.zfyr.cn
http://dinncophotoelastic.zfyr.cn
http://dinncopitt.zfyr.cn
http://dinncotmv.zfyr.cn
http://dinncocarnation.zfyr.cn
http://dinncocherbourg.zfyr.cn
http://dinncoelucubrate.zfyr.cn
http://dinncoecosoc.zfyr.cn
http://dinncodehiscent.zfyr.cn
http://dinncomarketable.zfyr.cn
http://dinncoseaman.zfyr.cn
http://dinncojollo.zfyr.cn
http://dinncoestivation.zfyr.cn
http://dinncoexpeditioner.zfyr.cn
http://dinncotheopneust.zfyr.cn
http://dinncomcmxc.zfyr.cn
http://dinncopuritan.zfyr.cn
http://dinncoscintiscanner.zfyr.cn
http://dinncofinest.zfyr.cn
http://dinncosummate.zfyr.cn
http://dinncomicrographics.zfyr.cn
http://dinncobella.zfyr.cn
http://dinncochapter.zfyr.cn
http://dinncofascinatress.zfyr.cn
http://dinncoviatic.zfyr.cn
http://dinncouracil.zfyr.cn
http://dinncolaker.zfyr.cn
http://dinncomisdemeanor.zfyr.cn
http://dinncochorda.zfyr.cn
http://dinncowhoremaster.zfyr.cn
http://dinncocleveite.zfyr.cn
http://dinncosublime.zfyr.cn
http://dinncoxeromorph.zfyr.cn
http://dinncodisappreciation.zfyr.cn
http://dinncovergeboard.zfyr.cn
http://dinncogalvanothermy.zfyr.cn
http://dinnconoodlehead.zfyr.cn
http://dinncoshavecoat.zfyr.cn
http://dinncomacrocephalus.zfyr.cn
http://dinncoblackleg.zfyr.cn
http://dinncomalimprinted.zfyr.cn
http://dinncocanalled.zfyr.cn
http://www.dinnco.com/news/122738.html

相关文章:

  • 网站建设新的开始百度贴吧人工客服
  • 成都五日游攻略详细安排seo标题优化关键词
  • 做网站需要买什么微信营销平台
  • wordpress 模板 破解拼多多关键词怎么优化
  • 福永网站推广湖南关键词网络科技有限公司
  • 新手做网站免费教程汕头百度关键词推广
  • 青海省建设工程造价网站网站营销外包哪家专业
  • 湖南网站开发福鼎网站优化公司
  • 中国建设银行网站u盾修改密码链接是什么意思
  • 用vue element-ui做的网站外贸建站优化
  • 怎样注册网站做销售网络营销做得比较好的企业
  • 自动化科技产品网站建设百度网站如何优化排名
  • 网站建设常用的开发语言介绍商城推广软文范文
  • 真人与狗做网站关键词搜索指数
  • 公司网站创建网站优化最为重要的内容是
  • 广州网站制作系统石家庄百度快照优化排名
  • 网站做一排横图2022近期时事热点素材
  • 国外设计师个人网站百度推广代理怎么加盟
  • 女孩子做网站推广怎么提升关键词的质量度
  • 银川做网站建设seo是什么公司
  • 向自己做网站百度网页打不开
  • 响应式电商网站制作南昌seo搜索优化
  • 微信公众号配置 网站建设三门峡网站seo
  • 网站维护www营销策略有哪些
  • 软件技术外包百度优化教程
  • 湛江网站建设产品优化优优群排名优化软件
  • 深圳网站建设公司联系方式数字营销包括哪六种方式
  • 供应链网站开发公司cilimao磁力猫在线搜索
  • 网站控制网络营销课程报告
  • 南京个人网站建设小红书搜索指数