Python-->> Fourth day

Source: Internet
Author: User

I. 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:

    1. 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
    2. A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end
    3. You can't go back halfway through the interview.
    4. Facilitates recycling of large data sets, saving memory
names = iter (['a','b','C' ])   #迭代器, the print (names) print (names.__next__ ())   #  prints the first element. Applies to open files after opening the operation. Print (names.__next__ ()) print (names.__next__ ())
The application of iterators, __next__ is to use this function to read the contents in turn.

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

def bash_dd (amount): #一个函数调用时返回一个迭代器, then this function is called the Generator (generator), if the function contains the yield syntax, then this function will become the generator whileAmount >0: Amount-= -        yield  -Print ('Meide')Get= BASH_DD ( -) #因为有yield所以返回的函数就为print (Type (Get)) Print (Get. __next__ ()) Print (Get. __next__ ()) Print ('Dabaojian') Print (Get. __next__ ()) # #返回值为100,'meide,100','Dabaojian','meide,100'
Generator Generator
defcash_out (amount): whileAmount >0:amount-= 1yield1<br>Print("wipe, again to collect money ... Black sheep! ") ATM= Cash_out (5) Print("take money to%s million"% ATM.__next__())Print("take the flowers out!")Print("take money to%s million"% ATM.__next__())Print("take money to%s million"% ATM.__next__())Print("take the flowers out!")Print("take money to%s million"% ATM.__next__())Print("take money to%s million"% ATM.__next__())Print("take money to%s million"% ATM.__next__())#Then the money will be taken out, and then take the errorPrint("take money to%s million"% ATM.__next__())
you can read it.

Python-->> Fourth day

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.