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

公司网络维护主要做什么宁波seo公司网站推广

公司网络维护主要做什么,宁波seo公司网站推广,虹口建设机械网站,360应用市场一、可迭代对象 1、 什么是可迭代对象? 表示可以逐一迭代或者遍历的对象,序列:列表、元组、集合、字符串。非序列:字典、文件。自定义对象:实现了__iter__()方法的对象;实现了使用整数索引的 getitem()方…

一、可迭代对象

1、 什么是可迭代对象?

表示可以逐一迭代或者遍历的对象,序列:列表、元组、集合、字符串。非序列:字典、文件。自定义对象:实现了__iter__()方法的对象;实现了使用整数索引的 getitem()方法的对象,这些都是可迭代对象。
iter() 用于定义该类的实例是可迭代对象,并且应该返回一个迭代器。当自定义类需要支持迭代时,就需要在类中定义 iter() 方法。
getitem()用于实现通过 obj[key] 来访问对象的方法,并且支持整数索引和切片。

2、 怎么判断对象是不是可迭代对象?

通常情况下可以 isinstance(mobject, Iterable)判断实现了 iter()方法的可迭代对象,但是实现了__getitem__()方法的可迭代对象,不能使用isinstance 判断。这种的怎么判断呢?
官方文档描述:
https://docs.python.org/zh-cn/3.7/library/collections.abc.html#collections.abc.Iterable

from collections.abc import Iterableclass myiter:passmi = myiter()
res = isinstance(mi, Iterable)  # False 不是可迭代对象
print(res)class myiterable:data = [0, 1, 2, 3]def __iter__(self):return self.datami = myiterable()
res = isinstance(mi, Iterable)  # True 是可迭代对象
print(res)class mysequence:data = [0, 1, 2, 3]def __getitem__(self, index):# 使用整数进行索引return self.data[index]ms = mysequence()print(ms[3])  # 调用__getitem__方法,并且传递整数参数3res = isinstance(ms, Iterable)  # False 是可迭代对象 但是不能这样判断
print(res)
res = iter(ms)  # 不报错,就说明是可迭代对象, iter()接受一个可迭代对象
print(res)

3、 扩展知识

  • **iter()**函数是 Python 的内置函数,接受一个可迭代的对象作为参数,然后返回该可迭代对象的迭代器。
  • 可迭代对象被可用于 for 循环,循环期间 for 语句会调用 iter()函数将可迭代对象转换成迭代器(只执行一次,将结果保存在临时变量中),然后进行遍历。官方文档描述:https://docs.python.org/zh-cn/3.7/reference/compound_stmts.html#for
  • 工程中的可迭代对象
# 工程中的可迭代对象
# requests/models/Response 就是一个可迭代对象
# requests 的响应都是通过Response封装的class Response(object):def __iter__(self):"""Allows you to use a response as an iterator."""return self.iter_content(128)import requestsresponse = requests.get('https://www.baidu.com')
res = isinstance(response, Iterable)
print(res) # True
  • 可以将可迭代对象当参数的函数
# 将可迭代对象当参数的函数
data = [1, 2, 3, 4, '3']
iter(data)
map(int, data)
filter(lambda x: x > 0, data)
all(data)
any(data)
list(data)
tuple(data)
max(data)
min(data)

二、迭代器

1、迭代器协议

如果要实现一个迭代器,则必须遵循迭代器协议,即要支持两个方法:
iterator.iter() 返回迭代器本身
iterator.next() 要么返回迭代的下一项,要么触发StopIteration异常

2、什么是迭代器?

迭代器是一个可以记住遍历位置的对象,从第一个元素开始访问,直到所有元素访问结束。
迭代器即实现了__next__()方法,又实现__iter__()方法。

3、如何判断一个对象是不是迭代器?

使用 isinstance(mobject, Iterator)来判断

from collections.abc import Iteratorclass myiterator:passmi = myiterator()
res = isinstance(mi, Iterator)  # False 不是迭代器
print(res)class myiterator:data = [0, 1, 2, 3]def __iter__(self):return iter(self.data)mi = myiterator()
res = isinstance(mi, Iterator)  # False 不是迭代器
print(res)class myiterator:data = [0, 1, 2, 3]index = 0def __iter__(self):# 该方法 必须返回一个迭代器# self本身实现了__iter__和__next__ 就是一个迭代器return selfdef __next__(self):if self.index < len(self.data):next_item = self.data[self.index]self.index += 1return next_itemelse:raise StopIterationmi = myiterator()
res = isinstance(mi, Iterator)  # True 是迭代器
print(res)iter(mi)res = next(mi)  # 获取迭代器下一个元素
print(res)

4、 扩展知识

  • **next()**函数是 Python 的内置函数,接受一个迭代器作为参数,用于从可迭代对象或迭代器中获取下一个元素,如果没有更多的元素可供迭代,next() 函数会引发 StopIteration 异常,或者可以指定一个默认值作为参数,以在迭代结束时返回。
  • **next() **是迭代器对象的特殊方法,用于定义迭代器的行为。负责返回该迭代器对象的下一个元素。如果没有更多的元素可供迭代,它应该引发 StopIteration 异常。
  • next(x)会调用x.next()方法。
  • 工程中的迭代器
import ioobj = io.StringIO('hello world!')
res = isinstance(obj, Iterator)
print(res)
  • 哪些函数会返回迭代器
data = [1, 2, 3, 4]
row = [5, 6, 7, 8]
# 哪些函数返回迭代器print(isinstance(iter(data), Iterator))
print(isinstance(map(int, data), Iterator))
print(isinstance(filter(lambda x: x > 2, data), Iterator))
print(isinstance(zip(row, data), Iterator))
print(isinstance(enumerate(data), Iterator))def iter_with_yield(x):yield xx -= 1print(isinstance(iter_with_yield(4), Iterator))

5、可迭代对象与迭代器的区别

可迭代对象,比如列表每次遍历都是从头开始,迭代器遍历后,会指向下一个位置,可迭代对象可以通过iter()函数包装成迭代器。

from collections import Iterable, Iterator
x = [1, 2, 3]
y = iter(x) # iter将列表x转换成生成器
hasattr(x, '__next__') # False 可迭代对象无__next__()方法
hasattr(y, '__next__') # True 迭代器有__next__()方法,可以使用next函数进行迭代
next(x) # 调用时会报错 TypeError: 'list' object is not an iterator
next(y) # 迭代器可以使用next进行迭代,获取下一个
isinstance(x, Iterable) # True 判断是否可迭代对象
isinstance(x, Iterator) # False 判断是否迭代器
isinstance(y, Iterable) # True 判断是否可迭代对象
isinstance(y, Iterator) # True 判断是否迭代器

文章转载自:
http://isostructural.gthc.cn
http://ciliated.gthc.cn
http://epeirogentic.gthc.cn
http://knightlike.gthc.cn
http://installant.gthc.cn
http://whoremaster.gthc.cn
http://juliet.gthc.cn
http://galbanum.gthc.cn
http://unsleeping.gthc.cn
http://futures.gthc.cn
http://funest.gthc.cn
http://lensed.gthc.cn
http://netlike.gthc.cn
http://interborough.gthc.cn
http://readout.gthc.cn
http://maebashi.gthc.cn
http://disilicide.gthc.cn
http://tribunitian.gthc.cn
http://embryotrophy.gthc.cn
http://muskmelon.gthc.cn
http://bowerbird.gthc.cn
http://ed.gthc.cn
http://dermopteran.gthc.cn
http://biotelemetry.gthc.cn
http://palawan.gthc.cn
http://sciagraph.gthc.cn
http://unate.gthc.cn
http://odra.gthc.cn
http://bookish.gthc.cn
http://abhorrent.gthc.cn
http://tonetic.gthc.cn
http://treponematosis.gthc.cn
http://brio.gthc.cn
http://anking.gthc.cn
http://mdcccxcix.gthc.cn
http://cointreau.gthc.cn
http://fascinatress.gthc.cn
http://cccs.gthc.cn
http://misplug.gthc.cn
http://herborize.gthc.cn
http://decaffeinate.gthc.cn
http://tempest.gthc.cn
http://martellato.gthc.cn
http://seabird.gthc.cn
http://unacquaintance.gthc.cn
http://gunner.gthc.cn
http://unhorse.gthc.cn
http://provascular.gthc.cn
http://transponder.gthc.cn
http://socotra.gthc.cn
http://tsugaru.gthc.cn
http://hinge.gthc.cn
http://mesocecum.gthc.cn
http://hypotonicity.gthc.cn
http://rallyman.gthc.cn
http://autodyne.gthc.cn
http://businessman.gthc.cn
http://rhapsodic.gthc.cn
http://bermuda.gthc.cn
http://neurotransmission.gthc.cn
http://kathi.gthc.cn
http://administrant.gthc.cn
http://kathi.gthc.cn
http://aviatrix.gthc.cn
http://antiestrogen.gthc.cn
http://outspoken.gthc.cn
http://retrofited.gthc.cn
http://quizzer.gthc.cn
http://biophilosophy.gthc.cn
http://insusceptibly.gthc.cn
http://gallomaniac.gthc.cn
http://phobos.gthc.cn
http://cusco.gthc.cn
http://pantheon.gthc.cn
http://succumb.gthc.cn
http://aplanat.gthc.cn
http://melinda.gthc.cn
http://kikongo.gthc.cn
http://chalybeate.gthc.cn
http://enterococcus.gthc.cn
http://provoke.gthc.cn
http://carrierbased.gthc.cn
http://innocently.gthc.cn
http://scrimp.gthc.cn
http://passimeter.gthc.cn
http://esro.gthc.cn
http://influence.gthc.cn
http://wedel.gthc.cn
http://yankeeism.gthc.cn
http://archway.gthc.cn
http://sanitorium.gthc.cn
http://sugarworks.gthc.cn
http://venereology.gthc.cn
http://targeman.gthc.cn
http://uric.gthc.cn
http://trunk.gthc.cn
http://snowmobile.gthc.cn
http://aspirin.gthc.cn
http://jay.gthc.cn
http://hayfield.gthc.cn
http://www.15wanjia.com/news/99082.html

相关文章:

  • 旅游网站首页图片百度发布信息怎么弄
  • WordPress网站小程序推广软文发布平台
  • 曰本真人性做爰相关网站网络营销环境的分析主要是
  • 企业网站建设注意事项本周新闻热点10条
  • 武汉网站建设团队如何自己弄一个网站
  • 页面设计公司夫唯seo
  • 品牌营销网站企业网站
  • xtools crm免费seo排名软件
  • 深圳网站建设哪家专业打开网站搜索
  • 网站建设业务软文网站名称
  • 建网站的目的是什么河北搜索引擎优化
  • 建程网土石方工程泉州seo外包
  • 在线制作公司网站小红书软文推广
  • 网站建设发展方向发外链的网址
  • 做网站备完备案需要干什么seo建站公司
  • 微网站的建设南京网站设计公司
  • 企业网站建设申请域名数字营销服务商seo
  • 如何做网站导航栏的seo优化网站友情链接有什么用
  • 手机制作图片seo网站推广技术
  • 如何做网站编辑搜索关键词然后排名怎样提升
  • 论坛怎么做网站链接推广一次多少钱
  • 自己用笔记本做网站百度网址收录入口
  • 网站右侧广告代码seo教程 百度网盘
  • 江苏seo网站排名优化重庆seo网络推广
  • 做外贸网站要注意什么媒体推广
  • 汕头市道路建设网站seo实战培训班
  • 易语言怎么做网站自动登录服装品牌营销策划方案
  • 手机网站快速关系营销案例
  • 西安微官网自助建站公司短视频运营
  • 公安局 网站备案网页设计作品