An iterator is a way to access collection elements. The iterator object is accessed from the first element of the set until all elements are accessed. The iterator can only move forward without moving back, but there is nothing to do with it, because people seldom go back on the way of iteration. In addition, the major advantage of the iterator is that it is not required to prepare all elements in the entire iteration process in advance. The iterator calculates an element only when it iterates to an element. before or after this, the element may not exist or be destroyed. This feature makes it especially suitable for traversing large or infinite sets, such as several G file iterators and generators.
1. iterator
An iterator is a way to access collection elements. The iterator object is accessed from the first element of the set until all elements are accessed. The iterator can only move forward without moving back, but there is nothing to do with it, because people seldom go back on the way of iteration. In addition, the major advantage of the iterator is that it is not required to prepare all elements in the entire iteration process in advance. The iterator calculates an element only when it iterates to an element. before or after this, the element may not exist or be destroyed. This feature makes it especially suitable for traversing large or infinite sets, such as several G files.
Features:
Visitors do not need to care about the internal structure of the iterator. they only need to use the next () method to retrieve the next content.
A value in the set cannot be randomly accessed, but can only be accessed from start to end in sequence
When half of the access traffic is reached, you cannot return back.
Easy to collect large data sets and save memory
>>> a = iter([1,2,3,4,5])>>> a
>>> a.__next__()1>>> a.__next__()2>>> a.__next__()3>>> a.__next__()4>>> a.__next__()5>>> a.__next__()Traceback (most recent call last): File "
", line 1, in
StopIteration
2. generator
When a function is called, an iterator is returned. This function is called a generator. if the function contains the yield syntax, this function will become a generator;
def func(): yield 1 yield 2 yield 3 yield 4
In the code above: func is a function called Generator. when you execute this function func (), you will get an iterator.
>>> temp = func()>>> temp.__next__()1>>> temp.__next__()2>>> temp.__next__()3>>> temp.__next__()4>>> temp.__next__()Traceback (most recent call last): File "
", line 1, in
StopIteration
3. instance
A. use the generator to customize the range
def xrange(n): start = 0 print(start) while True: if start > n : return yield start start += 1obj = xrange(4)n1 = obj.__next__()n2 = obj.__next__()n3 = obj.__next__()n4 = obj.__next__()n5 = obj.__next__()n6 = obj.__next__()print(n1,n2,n3,n4,n5,n6)
B. use the iterator to access range
The above is the details of the python-based iterator and generator. For more information, see other related articles in the first PHP community!