Python 简明教程
Python - Iterators
Python Iterators
Python 中的迭代器是一个对象,允许一次遍历一个元素,例如列表或元组等集合。它通过使用 iter() 和 next() 两个方法的实现来遵循迭代器协议。
An iterator in Python is an object that enables traversal through a collection such as a list or a tuple, one element at a time. It follows the iterator protocol by using the implementation of two methods iter() and next().
iter() 方法返回迭代器对象本身, next() 方法通过在没有更多可用元素时引发 StopIteration 异常来返回序列中的下一个元素。
The iter() method returns the iterator object itself and the next() method returns the next element in the sequence by raising a StopIteration exception when no more elements are available.
迭代器提供了一种在数据上进行迭代的内存高效方式,对大型数据集尤其有用。可以使用 iter() 函数从可迭代对象创建它们,或者使用自定义类和生成器实现它们。
Iterators provide a memory-efficient way to iterate over data, especially useful for large datasets. They can be created from iterable objects using the iter() function or implemented using custom classes and generators.
Iterables vs Iterators
在深入研究迭代表现之前,我们应该知道可迭代对象和迭代器之间的区别。
Before going deep into the iterator working, we should know the difference between the Iterables and Iterators.
-
Iterable: An object capable of returning its members one at a time (e.g., lists, tuples).
-
Iterator: An object representing a stream of data, returned one element at a time.
我们通常使用 for 循环来遍历可迭代对象,如下所示 −
We normally use for loop to iterate through an iterable as follows −
for element in sequence:
print (element)
Python 内置方法 iter() 实现 iter() 方法。它接收一个可迭代对象并返回迭代器对象。
Python’s built-in method iter() implements iter() method. It receives an iterable and returns iterator object.
Example of Python Iterator
以下代码从列表、字符串和元组等序列类型中获取迭代器对象。 iter() 函数还从字典中返回 keyiterator。
Following code obtains iterator object from sequence types such as list, string and tuple. The iter() function also returns keyiterator from dictionary.
print (iter("aa"))
print (iter([1,2,3]))
print (iter((1,2,3)))
print (iter({}))
它将生成以下 output −
It will produce the following output −
<str_iterator object at 0x7fd0416b42e0>
<list_iterator object at 0x7fd0416b42e0>
<tuple_iterator object at 0x7fd0416b42e0>
<dict_keyiterator object at 0x7fd041707560>
但是,int id 不可迭代,因此会产生 TypeError。
However, int id not iterable, hence it produces TypeError.
iterator = iter(100)
print (iterator)
它将生成以下 output −
It will produce the following output −
Traceback (most recent call last):
File "C:\Users\user\example.py", line 5, in <module>
print (iter(100))
^^^^^^^^^
TypeError: 'int' object is not iterable
Error Handling in Iterators
迭代器对象有一个名为 next() 的方法。每次调用它时,它都会返回迭代器流中的下一个元素。对 next() 函数的调用等同于调用迭代器对象的 next () 方法。
Iterator object has a method named next(). Every time it is called, it returns next element in iterator stream. Call to next() function is equivalent to calling next() method of iterator object.
此方法在没有更多要返回的项时引发 StopIteration 异常。
This method which raises a StopIteration exception when there are no more items to return.
Example
以下是我们创建的迭代器对象仅具有 3 个元素,我们对其进行超过三次迭代的示例 −
In the following is an example the iterator object we have created have only 3 elements and we are iterating through it more than thrice −
it = iter([1,2,3])
print (next(it))
print (it.__next__())
print (it.__next__())
print (next(it))
它将生成以下 output −
It will produce the following output −
1
2
3
Traceback (most recent call last):
File "C:\Users\user\example.py", line 5, in <module>
print (next(it))
^^^^^^^^
StopIteration
此异常可以在使用 try 和 except 块消耗迭代器的代码中捕获,尽管更常见的是借助如 for 循环等隐式处理它,该循环在内部管理 StopIteration 异常。
This exception can be caught in the code that consumes the iterator using try and except blocks, though it’s more common to handle it implicitly by using constructs like for loops which manage the StopIteration exception internally.
it = iter([1,2,3, 4, 5])
print (next(it))
while True:
try:
no = next(it)
print (no)
except StopIteration:
break
它将生成以下 output −
It will produce the following output −
1
2
3
4
5
Custom Iterator
Python 中的自定义迭代器是一个用户定义的类,它实现了迭代器协议,其中包含两个方法 iter() 和 next() 。这使得该类可以像迭代器那样工作,从而一次遍历它的元素。
A custom iterator in Python is a user-defined class that implements the iterator protocol which consists of two methods iter() and next(). This allows the class to behave like an iterator, enabling traversal through its elements one at a time.
要在 Python 中定义自定义迭代器类,该类必须定义这些方法。
To define a custom iterator class in Python, the class must define these methods.
Example
在以下示例中,Oddnumbers 是一个实现了 iter () 和 next () 方法的类。对 next () 的每一次调用都会将数字增加 2,从而在 1 到 10 的范围内流式传输奇数。
In the following example, the Oddnumbers is a class implementing iter() and next() methods. On every call to next(), the number increments by 2 thereby streaming odd numbers in the range 1 to 10.
class Oddnumbers:
def __init__(self, end_range):
self.start = -1
self.end = end_range
def __iter__(self):
return self
def __next__(self):
if self.start < self.end-1:
self.start += 2
return self.start
else:
raise StopIteration
countiter = Oddnumbers(10)
while True:
try:
no = next(countiter)
print (no)
except StopIteration:
break
它将生成以下 output −
It will produce the following output −
1
3
5
7
9
Example
让我们创建另一个迭代器,通过以下代码生成前 n 个斐波那契数:
Let’s create another iterator that generates the first n Fibonacci numbers with the following code −
class Fibonacci:
def __init__(self, max_count):
self.max_count = max_count
self.count = 0
self.a, self.b = 0, 1
def __iter__(self):
return self
def __next__(self):
if self.count >= self.max_count:
raise StopIteration
fib_value = self.a
self.a, self.b = self.b, self.a + self.b
self.count += 1
return fib_value
# Using the Fibonacci iterator
fib_iterator = Fibonacci(10)
for number in fib_iterator:
print(number)
它将生成以下 output −
It will produce the following output −
0
1
1
2
3
5
8
13
21
34
Asynchronous Iterator
Python 中的异步迭代器允许我们迭代异步序列,从而可以在循环中处理异步操作。
Asynchronous iterators in Python allow us to iterate over asynchronous sequences, enabling the handling of async operations within a loop.
它们遵循异步迭代器协议,其中包含方法 aiter() 和 anext() (从 Python 3.10 版本开始添加)。这些方法与 async for 循环结合使用,对异步数据源进行迭代。
They follow the asynchronous iterator protocol which consists of the methods aiter() and anext() (added in Python 3.10 version onwards.). These methods are used in conjunction with the async for loop to iterate over asynchronous data sources.
aiter() 函数返回一个异步迭代器对象。它是经典迭代器的异步对应方。任何异步迭代器都必须支持 *_aiter() * 和 anext() 方法。这些方法由两个内置函数在内部调用。
The aiter() function returns an asynchronous iterator object. It is an asynchronous counter part of the classical iterator. Any asynchronous iterator must support *_aiter()* and anext() methods. These methods are internally called by the two built-in functions.
与经典迭代器一样,异步迭代器提供了一个对象流。当流用尽时,会引发 StopAsyncIteration 异常。
Like the classical iterator the asynchronous iterator gives a stream of objects. When the stream is exhausted, the StopAsyncIteration exception is raised.
Example
在下例中,声明了一个异步迭代器类 Oddnumbers。它实现了 aiter () 和 anext () 方法。在每次迭代中,都会返回下一个奇数,并且程序等待一秒钟,以便它可以异步地执行任何其他进程。
In the example give below, an asynchronous iterator class Oddnumbers is declared. It implements aiter() and anext() method. On each iteration, a next odd number is returned and the program waits for one second, so that it can perform any other process asynchronously.
import asyncio
class Oddnumbers():
def __init__(self):
self.start = -1
def __aiter__(self):
return self
async def __anext__(self):
if self.start >= 9:
raise StopAsyncIteration
self.start += 2
await asyncio.sleep(1)
return self.start
async def main():
it = Oddnumbers()
while True:
try:
awaitable = anext(it)
result = await awaitable
print(result)
except StopAsyncIteration:
break
asyncio.run(main())