iterators
Iterators are a way to access the elements of a collection. The iterator object is accessed from the first element of the collection until all of the elements have been accessed and finished. Iterators can only move forward without going backwards, but that's fine, because people seldom retreat in the middle of an iteration. In addition, one of the great advantages of iterators is that they do not require that all elements in the entire iteration be prepared in advance. An iterator computes an element only when it iterates over it, and before or after that, the element may not exist or be destroyed. This feature makes it ideal for traversing large or infinite collections, such as several G files
Characteristics:
- The visitor does not need to care about the structure inside the iterator, but simply continues to fetch the next content through the next () method
- A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
- You can't go back halfway through the interview.
- Facilitates recycling of large data sets, saving memory
To generate an iterator:
Names=iter ([11,22,33,44,55,66,77])Print(names)#only memory addresses can be displayedPrint(names.__next__())#take a value, go to the first valuePrint(names.__next__())#take a value, go to the second valuePrint('Start for') forIinchNames#take all values, actually starting from the third value Print(i)Print('try next')Print(names.__next__())#It's at the end, and then the value is an error.Output Result:<list_iterator Object at 0x00000000006eb550>1122Start for3344556677TryNexttraceback (most recent): File"d:/winter_py/practice/winshen_python/day4/001_iter.py", line 17,inch<module>Print(names.__next__()) Stopiteration
iterators
Repeated calls to the iterator's __next__()
method (or passing it to the built-in function next()
) return successive items in th E stream. When no more data was available a StopIteration
exception is raised instead. At this point, the iterator object was exhausted and any further calls to its __next__()
method just raise again StopIteration
.
Generator Generator
Definition: When a function call returns an iterator, the function is called the Generator (generator), and if the function contains the yield syntax, the function becomes the generator
Code:
defCash_money (amount): whileAmount>0:amount-=100yield100#as long as yield is the generator, the value returned by the generator is an iterator Print("We 're taking the money again.") ATM=cash_money (500)Print(Type (ATM))Print(ATM.__next__())Print(ATM.__next__())Print('Other thing')#To build the benefits of iterators, the function executes half after the other work, and after the work is done, proceed with the function. Print(ATM.__next__())Print(ATM.__next__())Print(ATM.__next__()) Output result:<class 'Generator'>100We 're taking the money again.100Other thing#notice where the yield is interrupted. Print again to withdraw money is to continue the time will be executed. We 're taking the money again.100We 're taking the money again.100We 're taking the money again.100
Generator
Role:
The main effect of this yield is that the function can be interrupted, and save the interrupt state, after the interruption, the code can continue to execute, after a period of time can also recall the function, from the last yield of the next sentence to start execution.
In addition, it can realize the effect of concurrency operation in single-threaded case by yield.
Python Learning notes 4--iterators, generators, adorners, recursion