0 Basic python-13.2 Manual iterations: ITER and Next

Source: Internet
Author: User

Let's talk about this chapter. Manual iterations: ITER and Next

1.next

We've already mentioned this next in the last section, and we're going to expand it here, outside of the __next__ () method, we can also use the built-in function next (file) to implement

Here are the two sets of code listings with the same functionality:

>>> handler=open (' output_file.txt ') >>> next (handler) ' aaaaa\n ' >>> Next (handler) ' Bbbbb\ N ' >>> Next (handler) ' ccccc\n ' >>> Next (handler) ' ddddd\n ' >>> Next (handler) ' Fffff ' >> > Next (Handler) Traceback (most recent):  File "<pyshell#6>", line 1, in <module>    next ( Handler) stopiteration>>> Handler.close ()

>>> handler=open (' output_file.txt ') >>> handler.__next__ () ' aaaaa\n ' >>> handler.__next__ () ' bbbbb\n ' >>> handler.__next__ () ' ccccc\n ' >>> handler.__next__ () ' ddddd\n ' >>> handler._ _next__ () ' Fffff ' >>> handler.__next__ () Traceback (most recent call last):  File "<pyshell#14>", line 1, in <module>    


Both sets of code implement the same function: The next line of the file is read, and the end of the two methods is the end of a catch stopiteration exception


2.iter

ITER is an iterator. Let's first cite an example:

>>> alist=[1,2,3]>>> iter=iter (alist) >>> iter.__next__ () 1>>> Next (Iter) 2

The above steps are:

1) Create a list

2) returns an iterator via the ITER () method

3) Call the __next__ method of the iterator, or use the built-in function next () to get the contents of the next


Now the question is, why does the above file iterator not use ITER?

Let's proceed to the above code to get the following example:


Through the is test we see that the list returns an iterator only after calling the ITER () function, and for the file, the open itself returns an iterator.

So, for the return of a file, it returns a file iterator


For a list of iterations, we summarize the following two methods:


3. Other built-in types of iterators

1) Range



Although the above two methods, but we still recommend the use of the following one, more convenient and concise


2) Dictionary


>>> for key in Adict.keys ():p rint (Key,adict[key]) C 3b 2a 1

>>> adict={' A ': 1, ' B ': 2, ' C ':3}>>> for key in Adict:print (Key,adict[key]) C 3b 2a 1

Through the comparison of the above methods, we recommend the use of the last one, concise and clear


Summary: This chapter focuses on the use of the ITER iterator and next, as well as examples of their applications in lists, files, dictionaries, and other built-in types.



This chapter is here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Basic python-13.2 Manual iterations: ITER and Next

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.