? Generators Generator

Source: Internet
Author: User

The generator object returns a value through its next method until the stopiteration exception is triggered.

All you need to do is create a generator like creating a function. It contains a yield statement, pyth

On recognizes yield and marks it as a generator. When a function is executed to execute the yield statement, it returns a value like a return statement. The only difference is that the python interpreter returns a stack guide for the next function.

Create a generator:

 1 def mygenerator(): 2     yield 1 3     yield 2 4     yield ‘a‘ 5  6 """     7 >>> mygenerator() 8 <generator object mygenerator at 0x00B18EB8> 9 >>> g = mygenerator()10 >>> next(g)11 112 >>> next(g)13 214 >>> next(g)15 ‘a‘16 >>> next(g)17 18 Traceback (most recent call last):19   File "<pyshell#5>", line 1, in <module>20     next(g)21 StopIteration22 >>> 23 ""
 1 >>> x = mygenerator() 2 >>> x.next() 3 1 4 >>> x.next() 5 2 6 >>> x.next() 7 ‘a‘ 8 >>> x.next 9 <method-wrapper ‘next‘ of generator object at 0x012EF288>10 >>> x.next()11 12 Traceback (most recent call last):13   File "<pyshell#12>", line 1, in <module>14     x.next()15 StopIteration16 >>> 

 

 

Let's write a function shorten with a character list parameter and return the abbreviated value of this list. The length is determined by the number of vowels. For example, if loremipsum has four vowels (O, E, I, u), the abbreviation of the next return will be the first four, dolorist --> dolo. There are two vowels, and the next one will return the first two characters.

1 # Coding = UTF-8 2 def shorten (string_list): 3 length = Len (string_list [0]) 4 5 for S in string_list: 6 length = yield s [: length] 7 8 mystringlist = ['loremipsum', 'lorist', 'ametfoobar'] 9 Export stringlist = shorten (mystringlist) 10 result = [] 11 12 try: 13 s = next (distinct stringlist) 14 result. append (s) 15 16 While true: 17 number_of_vovels = Len (filter (lambda letter: letter in 'aeiou', S )) 18 19 # character 20 for the next abbreviation # determined by the number of vowels of the previous string 21 s = stringlist. send (number_of_vovels) 22 result. append (s) 23 24 hours t stopiteration: 25 pass26 27 28 "29 >>> result30 ['loremipsum', 'dolo ', 'am'] 31 >>> 32 """

The send () method allows us to pass a value to the generator.

 

? Generators Generator

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.