Python is why my objects can be recycled only once cycle, the second cycle is gone gone gone, resolved to solve

Why is a question I will summarize this article, you may encounter a novice

Of course, I am also a novice, so this is my problem now

 
 

First to describe the problem I encountered

One day high dark and stormy night, I was as usual squatting roadside (hee hee hee), a girl suddenly ran hastily

He took my hand and asked: "Why Why is my only loop iteration loop first, second, third, fourth ... no stuff, and I iterations ah ....."

I saw, on this, hum, I reached out with one hand that a skilled line and took her into his arms, the error message and a hug is a hug in my arms, my result sheets soaked,

A look at the body is sweat ah .....

 
Without further ado, open practice open practice
 
First we have to figure out, this may be an iterative thing (that is, the cycle of this thing), that in the end it is the iterator it or iterables it
First a quick overview: iterator it, there is no time for the cycle, while iterables it, either you will be ten thousand times for output
[Refer here for something for i in iterable]
You might almost understand, do not worry we go down further in-depth
 
Differences and connections iterator (Iterator) and iterables (Iterable) of
 
Iterator (Iterator): It is characterized not know how many times to execute, so you can understand I do not know how many elements, each called once __next __ () method, it will take a step down, of course, is __next __ () The method can only go down, not rolled back, follow the iterator protocol.
Iterator protocol: containing the __iter __ () method, and the method returns the Iterator objects themselves; containing the __next __ () method, whenever the __next __ () method is called, returns the next value until the value is not accessible, this time will stopinteration thrown in.
 
Iterables (Iterable): It is characterized by the fact that the size of the length of the sequence has been determined (list, tuple, dict, string, etc.), can follow an iterative protocol.
Protocol may be iteratively: containing the __iter __ () method. Iteration and may be subject to the __iter __ () method returns a corresponding iterator. (Such as list corresponding iterator is list_iterator)
 
More simply, if the class is available to meet the protocol iteration has the __iter __ () can become when iterables, if the class has the __iter __ () and the __next __ () method can be called iterator
 
Or more simply:
An iterator object is free of the __next __ () method of each iteration, the iterations are object into iterators to traverse many times how many times is converted.
And if it is an iterator, then traversal is constantly calling __next __ () process, and this process is one-way, to the last will raise StopIteration terminate the traversal of the element, iterative runs out, there is no
 
 
You should be possible to understand this it ha ha ha ha
------------------------------------------------------------------------------
If there hotties do not understand what an iterator, then please read on
 
 
Iteration is a cycle
An iterator is just a holding place for iterative
Dictionary iterative sequence and can be (but is iterables)
iter () denote generate an iterator (iterator is disposable remember, if you attempt sex can be dangerous oh), can the sequence, Dictionary ah ... and so ended up iterator
next () indicates the completion of each cycle of the output value
 
>> string = 'BCDEFG'
>>> c = iter(string)
>>> next(c)
'B'
>>> next(c)
'C'
>>> next(c)
'D'
>>> next(c)
'E'
>>> next(c)
'F'
>>> next(c)
'G'
>>> next(c)    ##如果循环到最后,没有值了,则抛出一个StopIteration报错
Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    next(c)
StopIteration

Here it is drawing to a close .... If it helps you remember handsome Way We then give me a praise Oh (consequently do, wanted to set Chan)

Released three original articles · won praise 2 · views 29

Guess you like

Origin blog.csdn.net/m0_46397094/article/details/105336825