我是靠谱客的博主 善良白云,这篇文章主要介绍迭代器的形成及退出,现在分享给大家,希望可以做个参考。

#迭代是一个访问集合元素的方式
#迭代器是一个可以记住遍历位置的对象
#迭代器对象从集合的第一个元素开始访问,直到所有元素被访问完结束
#迭代器只能向前不会后退

#第一种集合数据类型:列表,元组,字典,集合,字符串等
#第二种:生成器
#统称为可迭代对象

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# from _collections_abc import Iterator,Iterable # # x = [1,2,3,'asd'] # print(isinstance(x,Iterable)) # print(isinstance(1000,Iterable)) # # y = [x for x in range(10)] # z = (x for x in range(10)) # # print(type(y)) # print(type(z)) # print(isinstance(z,Iterable)) #判断是不是为可迭代对象 # # print(iter(z)) # print(iter(y)) # print(isinstance(iter(y),Iterable)) # print(isinstance(iter(y),Iterator)) # print(isinstance(y,Iterator)) #以列表为例,列表是个可迭代对象,Iter这个方法获取的是列表内部的迭代器 #生成器是一个迭代器对象,列表或者集合等是可迭代的,但都不是迭代器 from _collections_abc import Iterable,Iterator class Mylist(object): def __init__(self): self.newlist = list() def append_item(self,data): ''' 该方法实现了增加一个元素至列表末尾 :param data: :return: ''' self.newlist.append(data) def __iter__(self): #只有加上这个iter才能成为可迭代对象,不然就是普通的类 ''' 当前类中,因为iter这个魔法方法获取的是__iter__方法中的迭代器 :return: ''' iterobj = MyIterator(self.newlist) return iterobj class MyIterator(object): #这个类实现的是迭代器类 def __init__(self,newlist): self.newlist = newlist #记录的是当前迭代的索引 self.current_index = 0 def __next__(self): #当前的魔法方法记录遍历的位置,第二个功能是记录需要获取的值 if self.current_index< len(self.newlist): self.current_index+=1 return self.newlist[self.current_index-1] else: raise StopIteration def __iter__(self): return self list1 = Mylist() print(isinstance(list1,Iterable)) print(iter(list1)) print('###########################') obj = MyIterator([1,2,3,4,5,6,7,8,9,10]) print(isinstance(obj,Iterable)) while True: try: value = next(obj) print(value) except StopIteration: print('迭代成功') break class Fibonacci: '''迭代器类''' def __init__(self,month): self.a = 0 self.b = 1 self.month = month self.n = 0 def __iter__(self): return self def __next__(self): if self.n < self.month: self.a,self.b = self.b,self.a+self.b # self.n = self.n +1 self.n += 1 return self.a else: raise StopIteration fi = Fibonacci(5) print(isinstance(fi,Iterable)) print(isinstance(iter(fi),Iterator)) for i in fi: print(i)

注意点:
1、iter()方法访问的是迭代器内部的__iter__魔法方法,__iter__这个魔法方法,直接返回迭代器对象本身,也就是self
2、next()方法访问的是迭代器内部的__next__魔法方法,需要记录数据的位置以及抛出数据,所以,抛出数据的逻辑写在当前的方法
3、注意:__next__一定要判断如果数据已经完全取出的时候,要主动抛出错误 raise StopIteration

stopiteration退出迭代器for循环

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class MyIterator(object): #这个类实现的是迭代器类 def __init__(self,newlist): self.newlist = newlist #记录的是当前迭代的索引 self.current_index = 0 def __next__(self): #当前的魔法方法记录遍历的位置,第二个功能是记录需要获取的值 if self.current_index< len(self.newlist): self.current_index+=1 return self.newlist[self.current_index-1] else: raise StopIteration #为了让for循环自动退出 def __iter__(self): return self 注意点: raise StopIteration #为了让for循环自动退出,不然不断返回none

最后

以上就是善良白云最近收集整理的关于迭代器的形成及退出的全部内容,更多相关迭代器内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部