This article mainly introduces how to use the python iterator. the code is very simple. you can refer
What is an iterator?
The iterator is a simple object with the next method. of course, the _ iter _ function must be implemented. The iterator can iterate on the values of a series. when there is no iteration available, the next method will cause the StopIteration exception. In python, many objects are called iterators, such as lists, elements, strings, files, mappings, and collections.
How to use the iterator?
1. for variable in iteratable object
The code is as follows:
List1 = [1, 2, 4, 5]
For ele in list1:
Print ele,
Result: 1 2 3 4 5
2. if variable in iteratable object
The code is as follows:
List1 = [1, 2, 4, 5]
Var = 1
If var in list1:
Print 'Yes! '
Else:
Print 'no'
3. variable = iter (iteratable object)
The code is as follows:
It = iter ([1, 2, 4])
Print it. next ()
Print it. next ()
Print it. next ()
Result:
1
2
3
The iterator is an object.