What is an iterator? It is a stateful object that returns the next value in the container when you invoke the next()
method, and any object that implements the __iter__
and __next__()
(implemented in Python2 next()
) method is an iterator that returns the __iter__
iterator itself, __next__
returning the next value in the container. If there are no more elements in the container, the stopiteration exception is thrown. An iterative
object implements the __iter__
method that returns an Iterator object
Example 1:
Example 2:
known list namelist = [' Wang Yuan ', ' jialing ', ' curved ', ' Ouyang Nana ', ' Deng Chao ', ' chenhe ', ' Luhan ', ' Zheng Kai ', ' Franklin Wang Blue ', ' Angelababy ', ' Li Chen '] , delete list removelist = [' Wang Yuan ', ' jialing ', ' curved ', ' Ouyang Nana ',' Song '], requires that each element in the Removelist list be removed from the NameList ( Belonging to the removelist, but not belonging to the NameList ignore can be)
Error answer
#!/usr/bin/env pythonnamelist = [' Wang Yuan ', ' jialing ', ' curved ', ' Ouyang Nana ', ' Deng Chao ', ' chenhe ', ' Luhan ', ' Zheng Kai ', ' Franklin Wang Blue ', ' Angelababy ', ' li chen ']removelist = [' Wang Yuan ', ' jialing ', ' curved ', ' Ouyang Nana ', ' Song ']for name in NameList: if name in Removelist: namelist.remove (name) print (namelist)
Execution Result:
The above is not the result we want, edit the code, test to see the code execution process:
#!/usr/bin/env pythonnamelist = [' Wang Yuan ', ' jialing ', ' curved ', ' Ouyang Nana ', ' Deng Chao ', ' chenhe ', ' Luhan ', ' Zheng Kai ', ' Franklin Wang Blue ', ' Angelababy ', ' li chen ']removelist = [' Wang Yuan ', ' jialing ', ' curved ', ' Ouyang Nana ', ' Song ']for name in NameList: print (name) if name in removelist: Print ( Name) namelist.remove (name) print (namelist)
Execution Result:
From the above implementation we see that for sequence loops only match to ' Wang Yuan ', ' curved ', and this is why.
In fact, the result is that the sequence NameList is an iterative object, and the for loop is matched in list order. The first element in the list ' Wang Yuan ' matches first, then executes the code block remove operation, removing the element ' Wang Yuan ' from the original list namelist.
Note: The list is now updated to
[' jialing ', ' curved ', ' Ouyang Nana ', ' Deng Chao ', ' chenhe ', ' Luhan ', ' Zheng Kai ', ' Franklin Wang ', ' Angelababy ', ' Li Chen '], this time into the second for loop, there __next__返回的是列表中的下一个值‘沈腾‘,列表首个元素‘贾玲‘被忽略了。如此循环直到
are no more elements in the execution container , stop, no longer loops.
correct answer:
1 #!/usr/bin/env python2NameList = ['Wang Yuan','jialing','Curved','Ouyang Nana','Deng Chao','Chenhe','Luhan','Zheng Kai','Wang Zulan','Angelababy','Li Chen']3Removelist = ['Wang Yuan','jialing','Curved','Ouyang Nana','Song']4 5 forNameinchremovelist:6 Print(name)7 ifNameinchNameList:8 namelist.remove (name)9 Print(NameList)
View Code
Execution Result:
Learn about Python iterators from lists and instances