迭代器
迭代是 Python 最强大的功能之一,是访问集合元素的一种方式。
迭代器是一个可以记住遍历的位置的对象。
迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退。
迭代器有两个基本的方法:iter() 和 next()。
字符串,列表或元组对象都可用于创建迭代器:
实例(Python 3.0+)
>>> list=[1,2,3,4] >>> it = iter(list) # 创建迭代器对象 >>> print (next(it)) # 输出迭代器的下一个元素 1 >>> print (next(it)) 2 >>>
迭代器对象可以使用常规for语句进行遍历:
实例(Python 3.0+)
#!/usr/bin/python3 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 for x in it: print (x, end=" ")
执行以上程序,输出结果如下:
1 2 3 4
也可以使用 next() 函数:
实例(Python 3.0+)
#!/usr/bin/python3 import sys # 引入 sys 模块 list=[1,2,3,4] it = iter(list) # 创建迭代器对象 while True: try: print (next(it)) except StopIteration: sys.exit()
执行以上程序,输出结果如下:
1
2
3
4
创建一个迭代器
把一个类作为一个迭代器使用需要在类中实现两个方法 __iter__() 与 __next__() 。
如果你已经了解的面向对象编程,就知道类都有一个构造函数,Python 的构造函数为 __init__(), 它会在对象初始化的时候执行。
更多内容查阅:Python3 面向对象
__iter__() 方法返回一个特殊的迭代器对象, 这个迭代器对象实现了 __next__() 方法并通过 StopIteration 异常标识迭代的完成。
__next__() 方法(Python 2 里是 next())会返回下一个迭代器对象。
创建一个返回数字的迭代器,初始值为 1,逐步递增 1:
实例(Python 3.0+)
class MyNumbers: def __iter__(self): self.a = 1 return self def __next__(self): x = self.a self.a += 1 return x myclass = MyNumbers() myiter = iter(myclass) print(next(myiter)) print(next(myiter)) print(next(myiter)) print(next(myiter)) print(next(myiter))
执行输出结果为:
1
2
3
4
5
StopIteration
StopIteration 异常用于标识迭代的完成,防止出现无限循环的情况,在 __next__() 方法中我们可以设置在完成指定循环次数后触发 StopIteration 异常来结束迭代。
在 20 次迭代后停止执行:
实例(Python 3.0+)
class MyNumbers: def __iter__(self): self.a = 1 return self def __next__(self): if self.a <= 20: x = self.a self.a += 1 return x else: raise StopIteration myclass = MyNumbers() myiter = iter(myclass) for x in myiter: print(x)
执行输出结果为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
生成器
在 Python 中,使用了 yield 的函数被称为生成器(generator)。
yield 是一个关键字,用于定义生成器函数,生成器函数是一种特殊的函数,可以在迭代过程中逐步产生值,而不是一次性返回所有结果。
跟普通函数不同的是,生成器是一个返回迭代器的函数,只能用于迭代操作,更简单点理解生成器就是一个迭代器。
当在生成器函数中使用 yield 语句时,函数的执行将会暂停,并将 yield 后面的表达式作为当前迭代的值返回。
然后,每次调用生成器的 next() 方法或使用 for 循环进行迭代时,函数会从上次暂停的地方继续执行,直到再次遇到 yield 语句。这样,生成器函数可以逐步产生值,而不需要一次性计算并返回所有结果。
调用一个生成器函数,返回的是一个迭代器对象。
下面是一个简单的示例,展示了生成器函数的使用:
实例
def countdown(n): while n > 0: yield n n -= 1 # 创建生成器对象 generator = countdown(5) # 通过迭代生成器获取值 print(next(generator)) # 输出: 5 print(next(generator)) # 输出: 4 print(next(generator)) # 输出: 3 # 使用 for 循环迭代生成器 for value in generator: print(value) # 输出: 2 1
以上实例中,countdown 函数是一个生成器函数。它使用 yield 语句逐步产生从 n 到 1 的倒数数字。在每次调用 yield 语句时,函数会返回当前的倒数值,并在下一次调用时从上次暂停的地方继续执行。
通过创建生成器对象并使用 next() 函数或 for 循环迭代生成器,我们可以逐步获取生成器函数产生的值。在这个例子中,我们首先使用 next() 函数获取前三个倒数值,然后通过 for 循环获取剩下的两个倒数值。
生成器函数的优势是它们可以按需生成值,避免一次性生成大量数据并占用大量内存。此外,生成器还可以与其他迭代工具(如for循环)无缝配合使用,提供简洁和高效的迭代方式。
执行以上程序,输出结果如下:
https://avg.163.com/topic/detail/12480797
https://avg.163.com/topic/detail/12480873
https://avg.163.com/topic/detail/12480808
https://avg.163.com/topic/detail/12480797
https://avg.163.com/topic/detail/12480569
https://avg.163.com/topic/detail/12480562
https://avg.163.com/topic/detail/12480565
https://avg.163.com/topic/detail/12480489
https://avg.163.com/topic/detail/12480477
https://avg.163.com/topic/detail/12480474
https://avg.163.com/topic/detail/12479962
https://avg.163.com/topic/detail/12480248
https://avg.163.com/topic/detail/12480206
https://avg.163.com/topic/detail/12479960
https://avg.163.com/topic/detail/12479973
https://avg.163.com/topic/detail/12479962
https://avg.163.com/topic/detail/12479960
https://avg.163.com/topic/detail/12479682
https://avg.163.com/topic/detail/12479680
https://avg.163.com/topic/detail/12479694
https://avg.163.com/topic/detail/12479576
https://avg.163.com/topic/detail/12479375
https://avg.163.com/topic/detail/12479467
https://avg.163.com/topic/detail/12479451
https://avg.163.com/topic/detail/12479392
https://avg.163.com/topic/detail/12479382
https://avg.163.com/topic/detail/12479389
https://avg.163.com/topic/detail/12479375
https://avg.163.com/topic/detail/12479319
https://avg.163.com/topic/detail/12479297
https://avg.163.com/topic/detail/12479305
https://avg.163.com/topic/detail/12479223
https://avg.163.com/topic/detail/12479180
https://avg.163.com/topic/detail/12479170
https://avg.163.com/topic/detail/12479147
https://avg.163.com/topic/detail/12479143
https://avg.163.com/topic/detail/12479041
https://avg.163.com/topic/detail/12479025
https://avg.163.com/topic/detail/12478984
https://avg.163.com/topic/detail/12478963
https://avg.163.com/topic/detail/12478924
https://avg.163.com/topic/detail/12478668
https://avg.163.com/topic/detail/12478899
https://avg.163.com/topic/detail/12478891
https://avg.163.com/topic/detail/12478894
https://avg.163.com/topic/detail/12478890
https://avg.163.com/topic/detail/12478702
https://avg.163.com/topic/detail/12478804
https://avg.163.com/topic/detail/12478777
https://avg.163.com/topic/detail/12478757
https://avg.163.com/topic/detail/12478738
https://avg.163.com/topic/detail/12478729
https://avg.163.com/topic/detail/12478702
https://avg.163.com/topic/detail/12478668
https://avg.163.com/topic/detail/12478667
https://avg.163.com/topic/detail/12478645
https://avg.163.com/topic/detail/12478646
https://avg.163.com/topic/detail/12478637
https://avg.163.com/topic/detail/12478627
https://avg.163.com/topic/detail/12478622
https://avg.163.com/topic/detail/12478411
https://avg.163.com/topic/detail/12478416
https://avg.163.com/topic/detail/12478294
https://avg.163.com/topic/detail/12477982
https://avg.163.com/topic/detail/12478356
https://avg.163.com/topic/detail/12478353
https://avg.163.com/topic/detail/12478352
https://avg.163.com/topic/detail/12478350
https://avg.163.com/topic/detail/12478294
https://avg.163.com/topic/detail/12478280
https://avg.163.com/topic/detail/12478209
https://avg.163.com/topic/detail/12478207
https://avg.163.com/topic/detail/12478091
https://avg.163.com/topic/detail/12478033
https://avg.163.com/topic/detail/12477976
https://avg.163.com/topic/detail/12477853
https://avg.163.com/topic/detail/12477978
https://avg.163.com/topic/detail/12477982
https://avg.163.com/topic/detail/12477980
https://avg.163.com/topic/detail/12477870
https://avg.163.com/topic/detail/12477655
https://avg.163.com/topic/detail/12477861
https://avg.163.com/topic/detail/12477853
https://avg.163.com/topic/detail/12477822
https://avg.163.com/topic/detail/12477750
https://avg.163.com/topic/detail/12477709
https://avg.163.com/topic/detail/12477559
https://avg.163.com/topic/detail/12477664
https://avg.163.com/topic/detail/12477655
https://avg.163.com/topic/detail/12477485
https://avg.163.com/topic/detail/12477559
https://avg.163.com/topic/detail/12477550
https://avg.163.com/topic/detail/12477320
https://avg.163.com/topic/detail/12477485
https://avg.163.com/topic/detail/12477422
https://avg.163.com/topic/detail/12477413
https://avg.163.com/topic/detail/12477393
https://avg.163.com/topic/detail/12477320
https://avg.163.com/topic/detail/12477276
https://avg.163.com/topic/detail/12477272
https://avg.163.com/topic/detail/12477278
https://avg.163.com/topic/detail/12477210
https://avg.163.com/topic/detail/12477011
https://avg.163.com/topic/detail/12477004
https://avg.163.com/topic/detail/12476633
https://avg.163.com/topic/detail/12476702
https://avg.163.com/topic/detail/12476882
https://avg.163.com/topic/detail/12476876
https://avg.163.com/topic/detail/12476862
https://avg.163.com/topic/detail/12476858
https://avg.163.com/topic/detail/12476867
https://avg.163.com/topic/detail/12476741
https://avg.163.com/topic/detail/12476720
https://avg.163.com/topic/detail/12476593
https://avg.163.com/topic/detail/12476680
https://avg.163.com/topic/detail/12476633
https://avg.163.com/topic/detail/12476593
https://avg.163.com/topic/detail/12476334
https://avg.163.com/topic/detail/12476502
https://avg.163.com/topic/detail/12476509
https://avg.163.com/topic/detail/12476496
https://avg.163.com/topic/detail/12476490
https://avg.163.com/topic/detail/12476350
https://avg.163.com/topic/detail/12476333
https://avg.163.com/topic/detail/12476334
https://avg.163.com/topic/detail/12476214
https://avg.163.com/topic/detail/12476129
https://avg.163.com/topic/detail/12476177
https://avg.163.com/topic/detail/12476129
https://avg.163.com/topic/detail/12476102
https://avg.163.com/topic/detail/12476087
https://avg.163.com/topic/detail/12476068
https://avg.163.com/topic/detail/12476061
https://avg.163.com/topic/detail/12475946
https://avg.163.com/topic/detail/12475755
https://avg.163.com/topic/detail/12475932
https://avg.163.com/topic/detail/12475936
https://avg.163.com/topic/detail/12475809
https://avg.163.com/topic/detail/12475755
https://avg.163.com/topic/detail/12475686
https://avg.163.com/topic/detail/12475669
https://avg.163.com/topic/detail/12475475
https://avg.163.com/topic/detail/12475649
https://avg.163.com/topic/detail/12475658
https://avg.163.com/topic/detail/12475331
https://avg.163.com/topic/detail/12475475
https://avg.163.com/topic/detail/12475409
https://avg.163.com/topic/detail/12475371
https://avg.163.com/topic/detail/12475346
https://avg.163.com/topic/detail/12475331
https://avg.163.com/topic/detail/12475056
https://avg.163.com/topic/detail/12475190
https://avg.163.com/topic/detail/12475236
https://avg.163.com/topic/detail/12475193
https://avg.163.com/topic/detail/12475177
https://avg.163.com/topic/detail/12475190
https://avg.163.com/topic/detail/12475179
https://avg.163.com/topic/detail/12475076
https://avg.163.com/topic/detail/12475056
https://avg.163.com/topic/detail/12475035
https://avg.163.com/topic/detail/12474559
https://avg.163.com/topic/detail/12474850
https://avg.163.com/topic/detail/12474837
https://avg.163.com/topic/detail/12474832
https://avg.163.com/topic/detail/12474324
https://avg.163.com/topic/detail/12474344
https://avg.163.com/topic/detail/12474815
https://avg.163.com/topic/detail/12474559
https://avg.163.com/topic/detail/12474503
https://avg.163.com/topic/detail/12474414
https://avg.163.com/topic/detail/12474429
https://avg.163.com/topic/detail/12474245
https://avg.163.com/topic/detail/12474206
https://avg.163.com/topic/detail/12474323
https://avg.163.com/topic/detail/12474200
https://avg.163.com/topic/detail/12474319
https://avg.163.com/topic/detail/12474324
https://avg.163.com/topic/detail/12474314
https://avg.163.com/topic/detail/12473997
https://avg.163.com/topic/detail/12474206
https://avg.163.com/topic/detail/12474200
https://avg.163.com/topic/detail/12474185
https://avg.163.com/topic/detail/12474186
https://avg.163.com/topic/detail/12474175
https://avg.163.com/topic/detail/12474010
https://avg.163.com/topic/detail/12474010
https://avg.163.com/topic/detail/12474005
https://avg.163.com/topic/detail/12473997
https://avg.163.com/topic/detail/12473969
https://avg.163.com/topic/detail/12473766
https://avg.163.com/topic/detail/12473952
https://avg.163.com/topic/detail/12473800
https://avg.163.com/topic/detail/12473793
https://avg.163.com/topic/detail/12473766
https://avg.163.com/topic/detail/12473715
https://avg.163.com/topic/detail/12473709
https://avg.163.com/topic/detail/12473718
https://avg.163.com/topic/detail/12473701
https://avg.163.com/topic/detail/12473425
https://avg.163.com/topic/detail/12473621
https://avg.163.com/topic/detail/12473623
https://avg.163.com/topic/detail/12473591
https://avg.163.com/topic/detail/12473588
https://avg.163.com/topic/detail/12473440
https://avg.163.com/topic/detail/12473456
https://avg.163.com/topic/detail/12473440
https://avg.163.com/topic/detail/12473425
https://avg.163.com/topic/detail/12359899
https://avg.163.com/topic/detail/12473421
https://avg.163.com/topic/detail/12473420
以下实例使用 yield 实现斐波那契数列:
实例(Python 3.0+)
#!/usr/bin/python3 import sys def fibonacci(n): # 生成器函数 – 斐波那契 a, b, counter = 0, 1, 0 while True: if (counter > n): return yield a a, b = b, a + b counter += 1 f = fibonacci(10) # f 是一个迭代器,由生成器返回生成 while True: try: print (next(f), end=" ") except StopIteration: sys.exit()
执行以上程序,输出结果如下:




