Description: Explanation of iterators in Python
iterator is the meaning of an iterator, and its function is to produce one data item at a time until it is not. This allows the loop to be processed in the for loop. What is the difference between it and the general sequence type (list, tuple, etc.)? It only returns one data item at a time, consuming less memory. But it needs to remember the current state in order to return to the next data item. It is an object that has a next () method. The sequence type holds all the data items, and their access is indexed.
In the previous example, like the difference between ReadLines and Xreadlines, ReadLines is a one-time read into memory, and Xreadlines is only reading a single line into memory at the moment, which means that, when reading the current line, Need to have the corresponding register to write down the current reading state, temporarily can first understand.
1. Example 1: Use the next () function to read the number of iterators
• The following can be demonstrated:
>>> A = ITER (range) #创建一个迭代器 >>> A<listiterator object at 0x7f251917ec10>>>> a.next () 0>>> a.next () 1>>> a.next () 2>>> a.next () 3>>> a.next () 4>>> A.next () 5 >>> a.next () 6>>> a.next () 7>>> a.next () 8>>> a.next () 9>>> A.next () Traceback (most recent): File "<stdin>", line 1, in <module>stopiteration
a stores the address in the iterator, and next () can read the element at the next position;
The next () function does not determine the end of the iterator, only the end of the iteration based on the output exception;
A simple explanation of the 2.readline
• File operations can also read each line through the next () function:
>>> f = file (' student_info.txt ') >>> f.next () ' stu1101 mingjia.xu [email protected] 263 Systemadmin 18810404260\r\n ' >>> f.next () ' stu1102 yangjiansong [email protected] a8music systemadmin 13601247960\r\n ' > >> f.next () ' stu1103 Zouxinkai [email protected] Jishubu systemadmin 1861214111 ' >>> f.next () Traceback ( Most recent call last): File "<stdin>", line 1, in <module>stopiteration
ReadLine also uses the above iteration to process the contents of the file, only ReadLine adds the exception handling function, therefore does not have the error:
>>> f = file (' student_info.txt ') >>> f.readline () ' stu1101 mingjia.xu [email protected] 263 Systemadmin 18810404260\r\n ' >>> f.readline () ' stu1102 yangjiansong [email protected] a8music systemadmin 13601247960\r\n ' >>> f.readline () ' stu1103 Zouxinkai [email protected] Jishubu systemadmin 1861214111 ' >>> f.readline () ‘‘
3. Iterate through the contents of the iterator
• The demo is as follows:
>>> A = ITER (range) >>> while True: ... a.next () ... 0123456789Traceback (most recent): File ' <stdin> ', line 2, in <module>stopiteration
• Can only be achieved through a similar while true loop;
• This method allows you to get the contents of the iterator and further process the exception case.
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1696889
"Python Tour" chapter II (ix): iterators