编程语言
首页 > 编程语言> > Python3 迭代器深入解析

Python3 迭代器深入解析

作者:互联网

第6章 函数

6.7 迭代器

可迭代对象iterable

首先可以使用isinstance(对象名,Iterable)验证某一对象是否为可迭代对象

# 验证字符串、元组、列表、字典、集合类型是否为可迭代对象
from collections.abc import Iterable
int1 = 12306
str1 = "MondayTuesdaywednesdayThursdayFridaySaturdaySunday"
tuple1 = ("Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday")
list1 = ["Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
dict1 = {"Monday": "星期一", "Tuesday": "星期二", "wednesday": "星期三", "Thursday": "星期四", "Friday": "星期五", "Saturday": "星期六",
         "Sunday": "星期日", }
set1 = {"Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
print(isinstance(int1, Iterable))
print(isinstance(str1, Iterable))
print(isinstance(tuple1, Iterable))
print(isinstance(list1, Iterable))
print(isinstance(dict1, Iterable))
print(isinstance(set1, Iterable))
output:
False
True
True
True
True
True
# 验证的结果是字符串、元组、列表、字典、集合类型都是可迭代对象,数值不是可迭代对象。

可迭代对象的意思就是说这个实体是可迭代的,例如字符串、列表、元组、字典、集合、迭代器等等,可以用for ... in进行循环,

# 那么都可用for循环进行迭代取值,以元组为例
tuple1 = ("Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday")
for i in tuple1:
    print(i,end=',')
output:
Monday,Tuesday,wednesday,Thursday,Friday,Saturday,Sunday,

可以使用for循环迭代的标志是每个对象内部实现了__iter__方法,验证下每个可迭代对象是否有这个方法

str1 = "MondayTuesdaywednesdayThursdayFridaySaturdaySunday"
tuple1 = ("Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday")
list1 = ["Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
dict1 = {"Monday": "星期一", "Tuesday": "星期二", "wednesday": "星期三", "Thursday": "星期四", "Friday": "星期五", "Saturday": "星期六", "Sunday": "星期日", }
set1 = {"Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
print(("__iter__" in dir(str1)))
print(("__iter__" in dir(tuple1)))
print(("__iter__" in dir(list1)))
print(("__iter__" in dir(dict1)))
print(("__iter__" in dir(set1)))
output:
True
True
True
True
True

可迭代对象不是迭代器,如何变成迭代器呢?

# 可迭代对象通过调用__iter__方法就变成迭代器对象
tuple1 = ("Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday")
t1 = tuple1.__iter__()
print(t1)
output:
<tuple_iterator object at 0x0000027B44442EC8> # iterator的意思是迭代器

先稍微总结下,可迭代对象都有__iter__方法,而没有__next__方法。迭代器是怎么来的?是通过可迭代对象调用__iter__方法来生成一个迭代器对象,那么迭代器对象就拥有了__iter____next__方法。

# 验证下迭代器是否有__next__方法
tuple1 = ("Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday")
t1 = tuple1.__iter__()
print(t1)
print("__next__" in dir(t1))
output:
<tuple_iterator object at 0x000001D7F73E2EC8>
True

可迭代对象可通过索引取值、遍历取值,那么迭代器如何取值呢?

# 迭代器使用next()取值
# tuple1 = ("Monday", "Tuesday", "wednesday", "Thursday", "Friday", "Saturday", "Sunday")
t1 = tuple1.__iter__()
print(t1)
print(t1.__next__())
print(t1.__next__())
print(t1.__next__())
print(t1.__next__())
print(t1.__next__())
print(t1.__next__())
print(t1.__next__())
output:
<tuple_iterator object at 0x000001D7F73E2EC8>
Monday
Tuesday
wednesday
Thursday
Friday
Saturday
Sunday
StopIteration 
# 最后一次取值时报错了,next一次取一个值,取完后来通过报StopIteration这个错误结束。

讲了这么多到底什么是迭代器?

在Python中,迭代器是遵循迭代协议的对象,通过调用可迭代的对象的__iter__方法生成迭代器对象,迭代器对象具有__next__方法,可通过此方法对迭代器取值。

这时能理解for语句如何实现遍历的吗?for语句通过调用可迭代对象的__iter__方法,这个方法返回一个迭代器,for语句循环调用迭代器内的每个元素的__next__方法,当迭代器为空时,__next__方法会引发一个StopIteration异常,这个异常会告诉for语句终止循环。

迭代器到底有什么用呢?

节约内存空间:文本文件本身是迭代器对象,用迭代器的方式访问文件时,可以每次读取一行,而不必一次性将整个文件读入,节约内存。

# r1.txt文本文件内容
"""
Monday
Tuesday
wednesday
Thursday
Friday
Saturday
Sunday
"""

r1 = open("r1.txt")
print(r1.__next__(), end="")
print(r1.__next__(), end="")
output:
Monday
Tuesday

迭代

讲完迭代器后,迭代就比较好理解了,迭代就是从迭代器中取元素的过程

比如我们用for循环从train = "12306"中取元素,这种遍历过程就被称作迭代

# for循环的对象必须是可迭代对象,如果这个对象不是,那么会报错
train = 12306
for i in train:
    print(i)
output:
TypeError: 'int' object is not iterable

如果你不想用for循环迭代呢?这时你可以:

  1. 先调用容器(以字符串为例)的iter()函数
  2. 再使用 next() 内置函数来调用 next() 方法
  3. 当元素用尽时,next() 将引发 StopIteration 异常

# 定义一个字符串
>>> train = "12306"
>>> it = iter(train)
>>> it
<str_iterator object at 0x000001E30D71BE48>
>>> next(it)
'1'
>>> next(it)
'2'
>>> next(it)
'3'
>>> next(it)
'0'
>>> next(it)
'6'
>>> next(it)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
StopIteration

# 用while循环模拟实现上面的过程
def while_iterator(iterable_obj):
    iterator_obj = iter(iterable_obj)
    while 1:
        try:
            print(next(iterator_obj), end=' ')
        except StopIteration:
            return
train = "12306"
while_iterator(train)
output:
1 2 3 0 6 

标签:__,Monday,迭代,iter,next,print,解析,Python3
来源: https://www.cnblogs.com/infuture/p/13305765.html