Oh, this is the real Cow x programmer. However, he is only a bull x, not the great God. What is a great god programmer like? He is a sweeping monk, big faint in the city.
Let's find out what these nouns are and say something else:
Loop, which refers to repeating the same piece of code when the condition is met. For example, a while statement.
iteration (iterate), which refers to each item in a list that is accessed individually in some order. For example, a for statement.
recursion (recursion) refers to the behavior of a function that constantly calls itself. For example, programmatically outputting the famous Fibonacci sequence.
Traversal (traversal), which refers to accessing each node in a tree structure according to certain rules, and each node is accessed only once.
For these four sounds inscrutable vocabulary, in the tutorial, has been involved in a-loop (loop), the main introduction of the iteration (iterate), crossing on the internet, Google, will find that the iteration and loop, recursive comparison between the article many, They were compared from different angles. There is no comparison here, first understand the iterations in Python. After the appropriate time to compare, if I do not forget, haha.
Access individually
In Python, you can do this by accessing each element in the object: (for example, a list)
Copy the Code code as follows:
>>> LST
[' Q ', ' I ', ' W ', ' s ', ' I ', ' R ']
>>> for I in LST:
... print I,
...
Q I w s i R
In addition to this approach, you can do this:
Copy the Code code as follows:
>>> lst_iter = iter (LST) #对原来的list实施了一个iter ()
>>> Lst_iter.next () #要不厌其烦地一个一个手动访问
' Q '
>>> Lst_iter.next ()
I
>>> Lst_iter.next ()
' W '
>>> Lst_iter.next ()
' s '
>>> Lst_iter.next ()
I
>>> Lst_iter.next ()
' R '
>>> Lst_iter.next ()
Traceback (most recent):
File " ", line 1, in
Stopiteration
As a good programmer, the best quality is "lazy", of course, can not hit one by one, so that:
Copy the Code code as follows:
>>> while True:
... print lst_iter.next ()
...
Traceback (most recent): #居然报错, and the error is the same as the front face? What reason?
File " ", Line 2, in
Stopiteration
>>> lst_iter = iter (LST) #那就再写一遍, the above error is put aside for the moment, looking back at the study
>>> while True:
... print lst_iter.next ()
...
Q #果然自动化地读取了
I
W
S
I
R
Traceback (most recent): #读取到最后一个之后, error, stop cycle
File " ", line 2, in
Stopiteration
>>>
First look at the built-in function used above: ITER (), which is described in the official document:
Copy the Code code as follows:
ITER (o[, Sentinel])
Return an Iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, o must be a collection object which supports the iteration protocol (the ITER () method), or it Must support the sequence protocol (the GetItem () method with an integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised. If The second argument, Sentinel, is given and then o must be a callable object. The iterator created in this case would call O with a arguments for each call to its next () method; If the value returned is equal to Sentinel, Stopiteration would be raised, otherwise the value would be returned.
To the effect that ... (here deliberately omitted some words, because I believe see this article of crossing English level is to see the level of the document, cleavage No, do not worry, find a dictionary what help. )
Although not translated, but also to refine the main things:
The return value is an iterator object
Parameter needs to be an object that conforms to the iteration protocol or is a sequence object
Next () Fit and use
What is an "iterative object"? In general, we often refer to objects that can be used for a single read element, which is called an iterative object. Then for is also called an iterative tool. The so-called iterative tool is the ability to scan each element of an iterative object in a certain order (in order from left to right), obviously, besides for, there are other things that can be called iterative tools, such as list parsing, in to determine whether an element belongs to a sequence object, and so on.
So, what about the function of the ITER () just introduced? It is used in conjunction with Next () and is also useful for implementing the above iterative tools. In Python, even in other languages, the iteration of this piece of the argument is chaotic, mainly the noun disorder, just now we said that those who can achieve the iteration, called the iterative tool, is these iterative tools, many programmers like to be called iterators. Of course, this is all Chinese translation, English is iterator.
Crossing See all the examples above will find that if you use for to iterate, when the end of the time, it will automatically end, no error. If you use ITER () ... next () iteration, when the last one is finished, it does not end automatically and continues downward, but there is no element behind it, so a mistake called Stopiteration is reported (the name of this error is called: Stop iteration, where is error, clearly warning).
Crossing also pay attention to a feature of the ITER () next () iteration. When the iteration object Lst_iter is iterated over, that is, after each element has been read one side, the pointer moves to the back of the last element. If again accessed, the pointer does not automatically return to the first position, but still stay in the end position, so the report stopiteration, want to start again, need to re-enter the iteration object. So, yours faithfully see, when I re-iterated the object assignment above, I can continue. This is not in the iteration tool for the for type.
File iterators
Now there is a file, name: 208.txt, whose contents are as follows:
Copy the Code code as follows:
Learn Python with Qiwsir.
There is the free Python course.
The website is:
Http://qiwsir.github.io
It language is Chinese.
Using an iterator to manipulate this file, we have already done this in the previous document about the knowledge, nothing more than:
Copy the Code code as follows:
>>> f = open ("208.txt")
>>> F.readline () #读第一行
' Learn Python with qiwsir.\n '
>>> F.readline () #读第二行
' There is free Python course.\n '
>>> F.readline () #读第三行
' The website is:\n '
>>> F.readline () #读第四行
' Http://qiwsir.github.io\n '
>>> f.readline () #读第五行, that is, after I read the last line, it's behind the line.
' It language is chinese.\n '
>>> f.readline () #无内容了, but no error, return empty.
''
The above shows a line of ReadLine () to read. Of course, in the actual operation, we are absolutely unable to do so, we must let it automatically, the more commonly used methods are:
Copy the Code code as follows:
>>> for line in F: #这个操作是紧接着上面的操作进行的, please crossing main observations
.. print line, #没有打印出任何东西
...
The code does not print anything because the pointer has moved to the end after the previous iteration. This is a feature of the iteration, be careful of the position of the pointer.
Copy the Code code as follows:
>>> f = open ("208.txt") #从头再来
>>> for line in F:
.. print line,
...
Learn Python with Qiwsir.
There is the free Python course.
The website is:
Http://qiwsir.github.io
It language is Chinese.
This method is commonly used to read files. Another ReadLines () is also possible. However, there is a need for some caution, crossing if you can't remember what to watch out for, you can review the course on the document side.
The above procedure can also be read with Next ().
Copy the Code code as follows:
>>> f = open ("208.txt")
>>> F.next ()
' Learn Python with qiwsir.\n '
>>> F.next ()
' There is free Python course.\n '
>>> F.next ()
' The website is:\n '
>>> F.next ()
' Http://qiwsir.github.io\n '
>>> F.next ()
' It language is chinese.\n '
>>> F.next ()
Traceback (most recent):
File " ", line 1, in
Stopiteration
If you use Next (), you can read the contents of each line directly. This indicates that the file is a natural, iterative object that does not need to be converted with ITER ().
Again, we use for to implement the iteration, in essence, is automatically called Next (), but this work, has let for secretly for us to dry, here, yours faithfully should give for to take another name: it is called Lei Feng.
As mentioned earlier, list parsing can also be an iterative tool, and in the study of the list, crossing must have been clear. So, is it possible to use the file? Give it a try:
Copy the Code code as follows:
>>> [line for line in open (' 208.txt ')]
[' Learn Python with qiwsir.\n ', ' There are free Python course.\n ', ' The website is:\n ', ' http://qiwsir.github.io\n ', ' its L Anguage is chinese.\n ']
At this point, crossing not for the list of resolution to be impressed? It's really strong, strong and big.
In fact, the iterator is far more than that simple, let's just list some, in Python you can also get the elements in the iteration object.
Copy the Code code as follows:
>>> List (open (' 208.txt '))
[' Learn Python with qiwsir.\n ', ' There are free Python course.\n ', ' The website is:\n ', ' http://qiwsir.github.io\n ', ' its L Anguage is chinese.\n ']
>>> Tuple (open (' 208.txt '))
(' Learn Python with qiwsir.\n ', ' There are free Python course.\n ', ' The website is:\n ', ' http://qiwsir.github.io\n ', ' its L Anguage is chinese.\n ')
>>> "$$$". Join (Open (' 208.txt '))
' Learn Python with qiwsir.\n$$ $There are free python course.\n$$ $The website is:\n$$ $http://qiwsir.github.io\n$$ $Its Language is chinese.\n '
>>> a,b,c,d,e = open ("208.txt")
>>> A
' Learn Python with qiwsir.\n '
>>> b
' There is free Python course.\n '
>>> C
' The website is:\n '
>>> D
' Http://qiwsir.github.io\n '
>>> E
' It language is chinese.\n '
The above approach, in programming practice is not necessarily used, just to show crossing, and crossing to understand, can do so, not to do so.
To add, the dictionary can also be iterative, crossing oneself may try to find out (in fact, the previous has been used for iterating over, this time please explore with ITER () ... next () Manual step by step iteration).