Learn about Python iterators from lists and instances

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.