python函数可迭代对象与迭代器
发布于 2021-04-17 04:44 ,所属分类:知识学习综合资讯
目录
可迭代对象
迭代器
可迭代对象与迭代器对比
详解
可迭代对象

(点击查看大图)
可迭代对象定义
字面意思:
对象:一个实实在在存在的值;
可迭代:更新迭代,重复的,循环的一个过程,更新迭代每次都有新的内容;
可迭代对象:可以进行循环更新的一个实实在在值。
专业角度:
内部含有'__iter__'方法的对象,如str、list、tuple、dic、set、range、文件句柄等。
优点:
存储的数据直接能显示,比较直观;
拥有的方法比较多,操作方便。
缺点:
占用内存;
不能迭代取值(除去索引,key以外)。
获取对象内部方法
可以通过dir() 去判断一个对象具有什么方法。dir()会返回一个列表,这个列表中含有该对象的以字符串的形式所有方法名。
s = 'pamela'print(dir(s))
(左右滑动查看完整代码)
判断对象是否是可迭代对象
可迭代对象可以通过判断该对象是否有’__iter__’方法来判断。
s = 'pamela'i = 1print('__iter__' in dir(s)) # Trueprint('__iter__' in dir(i)) # False
(左右滑动查看完整代码)
迭代器

(点击查看大图)
迭代器定义
字面意思:
器:工具;
迭代器:可更新迭代的工具。
专业角度:
内部含有'__Iter__'方法和'__next__'方法的对象,如文件句柄等。
优点:
节省内存;
迭代器在内存中相当于只占一个数据的空间,每次取值时上一条数据会在内存释放。
惰性机制。
next一次,取一个值,绝不过多取值。
迭代器模式:迭代是数据处理的基石。扫描内存中放不下的数据集时,就要找一种惰性获取数据项的方式,即按需一次获取一个数据项。
缺点:
速度慢;
不能直观的查看里面的数据;
取值时不走回头路,只能一直向下取值。
判断对象是否是迭代器
迭代器可以通过判断该对象是否有’__iter__’ 和 ‘__next__’ 方法来判断。
s='pamela'#字符串是可迭代对象,不是迭代器print('__iter__' in dir(s)) # Trueprint('__next__' in dir(s)) # Falsel = [1, 2, 3] # 列表是可迭代对象,不是迭代器print('__iter__' in dir(l)) # Trueprint('__next__' in dir(l)) # Falset = (1, 2, 3) # 元组是可迭代对象,不是迭代器print('__iter__' in dir(t)) # Trueprint('__next__' in dir(t)) # Falsedic = {'name': 'pamela', 'age': 18} # 字典是可迭代对象,不是迭代器print('__iter__' in dir(dic)) # Trueprint('__next__' in dir(dic)) # Falsest = {1, 2, 3} # 集合是可迭代对象,不是迭代器print('__iter__' in dir(st)) # Trueprint('__next__' in dir(st)) # Falser = range(3) # range是可迭代对象,不是迭代器print('__iter__' in dir(r)) # Trueprint('__next__' in dir(r)) # Falsef = open('file', 'w', encoding='utf-8') # 文件句柄既是可迭代对象,又是迭代器print('__iter__' in dir(f)) # Trueprint('__next__' in dir(f)) # Truef.close()
(左右滑动查看完整代码)
可迭代对象转化成迭代器
可迭代对象可以通过iter() 或者 __iter__() 方法转化成迭代器。
l = [1, 2, 3]print('__iter__' in dir(l)) # Trueprint('__next__' in dir(l)) # Falseobj = iter(l) # 把可迭代对象转化成迭代器# obj = l.__iter__()print('__iter__' in dir(obj)) # Trueprint('__next__' in dir(obj)) # Trueprint('__iter__' in dir(l)) # Trueprint('__next__' in dir(l)) # False
(左右滑动查看完整代码)
迭代器的取值
可迭代对象不可以迭代取值(除去索引,key以外),但是转化成迭代器就可以了,迭代器可以利用next() 或者 __next__()进行取值,一个next取对应的一个值,如果迭代器里面的值取完了,还要next进行取值,那么就会报StopIteration的错误。
l = [1, 2, 3]obj = iter(l) # 把可迭代对象转化成迭代器# obj = l.__iter__()ret = next(obj)# ret = obj.__next__()print(ret) # 1ret = next(obj)print(ret) # 2ret = next(obj)print(ret) # 3ret = next(obj)print(ret) # StopIteration
(左右滑动查看完整代码)
迭代器取值时不走回头路,只能一直向下取值。
l = [1, 2, 3, 4, 5, 6]obj = iter(l)for i in range(2):print(next(obj))for i in range(2):print(next(obj))'''执行结果是:1 2 3 4'''
(左右滑动查看完整代码)
while循环模拟for循环机制
for循环的循环对象一定要是可迭代对象,但是这不意味着可迭代对象就可以取值。
for循环的内部机制是:将可迭代对象转换成迭代器,然后利用next进行取值,最后利用异常处理 处理StopIteration抛出的异常。
l = [1, 2, 3]'''1.把可迭代对象转化成迭代器'''obj = iter(l)# obj = l.__iter__()'''2.利用while循环,next()进行取值'''while True:'''3.利用异常处理终止循环'''try:print(next(obj))# print(obj.__next__())except StopIteration:break
(左右滑动查看完整代码)
可迭代对象与迭代器对比

(点击查看大图)




![[Python] 11个具体的实际开发案例Python近期视频资料 如http服务器 QQ空间自动登录第三方函数](https://static.kouhao8.com/sucaidashi/xkbb/0ceab0a2df5e4d63fda958d69ac7f181.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)



![[Python] python web开发视频教程51集(基础+函数+实例)](https://static.kouhao8.com/sucaidashi/xkbb/7ae6c7e3e4fe91fc43ad7a9f3c1f08a3.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)




![[Python爬虫] Python两套专题培训视频教程 Python自定义函数+Python培训之图片下载爬虫](https://static.kouhao8.com/sucaidashi/xkbb/c5d2bb19e1f9dd55f599179051f766e3.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)
![[Python爬虫] Python两套专题培训视频教程 Python自定义函数+Python培训之图片下载爬虫](https://static.kouhao8.com/sucaidashi/xkbb/40e6fd07991b797c294a1efa0fc5662a.png?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)
![[Python] Python编程专题视频9集(操纵MySQL数据库+面向对象+方法+类+wxPython精讲)](https://static.kouhao8.com/sucaidashi/xkbb/c806fff23ba70cc479d33973a3e0ca49.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)
![[Python] Python自定义函数+Python培训之图片下载爬虫 Python两套专题培训视频教程](https://static.kouhao8.com/sucaidashi/xkbb/d5dd3afb5e3eb118e73400dda8792c48.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)
![[Python] xx学院 Python面向对象编程 视频教程 丁敬香主讲 视频教程下载分享](https://static.kouhao8.com/sucaidashi/xkbb/2230927587bdc6cd0db5222a0df56a1a.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)









![[Python] 中谷教育python中文视频教程(全38集)[简史+基础+函数+变量+正则表达式+爬虫+异常]](https://static.kouhao8.com/sucaidashi/xkbb/ea8bf1e8ef7957e6802322d8ef7b4782.jpg?x-oss-process=image/format,webp/resize,w_88/crop,w_88,h_88,g_nw)
相关资源