This article mainly introduces the Python iterator definition and simple usage, and analyzes the concept, principle, creation and use of the iterator in the form of an instance, and the friends can refer to the following
The examples in this article describe Python iterator definitions and simple usages. Share to everyone for your reference, as follows:
First, what is an iterator
Iterations, as the name implies, are repeated several times (as is now done in the loop). An iterator is an object that implements the __next__ () method, which does not require any arguments when called, and is a way to access an iterative sequence, usually starting from the first element of the sequence, until all elements are accessed before the end. [Note]: Iterators can only go forward and not back
[Advantages of iterators]:
Using iterators does not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and the element may not exist or be destroyed before or after it. Iterators are therefore suitable for traversing a number of large or even infinite sequences.
Second, create an iterator
A. Use the built-in factory function iter (iterable) to convert an iterative sequence into an iterator
a=[1,2,3,4]b= (str=) ' tomwenxing ' Print (ITER (a)) print (ITER (b)) print (ITER (str))
Operation Result:
<listiterator Object at 0x0000000001d6d550>
<tupleiterator Object at 0x0000000001d6d550>
<iterator Object at 0x0000000001d6d550>
B, Custom iterators
Essentially every invocation method of an iterator in python __next__() returns the next element or a container object that throws Stopiteration
• Because there is no "iterator" class in Python, a class with the following two attributes can be called an iterator class:
1. A __next__() method that returns the next element of the container or throws a Stopiteration exception
2, there are __iter__() methods to return the iterator itself
#斐波那契数列class fabs (): def __init__ (Self,max): self.max=max self.n,self.a,self.b=0,0,1 def __iter__ (self): #定义__iter__方法 return self def __next__ (self): #定义__next__方法 if Self.n<self.max: tmp= self.b self.a,self.b=self.b,self.a+self.b #等价于: #t = (self.a,self.a+self.b) #self. a=t[0] #self. b=t[1] self.n+=1 return tmp raise Stopiterationprint (Fabs (5)) for item in Fabs: print ( Item,end= ")
Operation Result:
<__main__. Fabs object at 0x00000000023f9278>
1 1 2 3 5 8 13 21 34 55
Three, the method of the iterator
1.ITER.__NEXT__ (): Returns the next element of the iterator, but throws a Stopiteration exception when there is no next element
List=[1,2,3,4]list=iter (list) print (list.__next__ ()) print (list.__next__ ()) print (list.__next__ ()) Print (list.__ next__ ()) print (list.__next__ ())
Operation Result:
Traceback (most recent):
File "E:\py3Demo\Hello\iterDemo.py", line 7, <module>
Print (list.__next__ ())
Stopiteration
1
2
3
4
2.ITER.__ITER__ (): Returns the Iterator object itself
List=[1,2,3,4]list=iter (list) print (list.__iter__ ())
Operation Result:
<list_iterator Object at 0x0000000001d6f208>