Python iterators and generators

Source: Internet
Author: User

Code:

1 #x= ' Hello '2 ## Print (dir (x))3 #iter_test=x.__iter__ ()4 #5 ## Print (iter_test)6 #print (iter_test.__next__ ())7 #print (iter_test.__next__ ())8 #print (iter_test.__next__ ())9 #print (iter_test.__next__ ())Ten #print (iter_test.__next__ ()) One  A # -l=[1,2,3] - #For i in L: #i_l =l.__iter_ () i_l.__next__ () the #print (i) -  - #index=0 - #While index < Len (l): + #print (L[index]) - #index+=1 +  A  at #iter_l=l.__iter__ () #遵循迭代器协议, generating an iterative object - #print (iter_l.__next__ ()) - #print (iter_l.__next__ ()) - # - #For i in L: - #print (i) in  - #s={1,2,3} to # + ## for I in S: - ## Print (i) the #iter_s=s.__iter__ () * #print (iter_s) $ #print (iter_s.__next__ ())Panax Notoginseng #print (iter_s.__next__ ()) - #print (iter_s.__next__ ()) the #print (iter_s.__next__ ()) +  A #dic={' A ': 1, ' B ': 2} the #iter_d=dic.__iter__ () + #print (iter_d.__next__ ()) -  $ #f=open (' test11.py ', ' r+ ') $ ## for I in F: - #iter_f=f.__iter__ () - #print (iter_f) the ## Print (iter_f.__next__ (), end= ") - #print (iter_f.__next__ (), end= ")Wuyi #l=[1,2,3,4,5] the #diedai_l=l.__iter__ () - #While True: Wu #Try: - #print (diedai_l.__next__ ()) About #except stopiteration: $ ## print (' iteration completed, loop terminated ') - # Break -  - #l=[' die ', ' Erzi ', ' Sunzi ', ' Chongsunzi '] A # + #iter_l=l.__iter__ () the #print (iter_l) - #print (iter_l.__next__ ()) $ #print (iter_l.__next__ ()) the #print (iter_l.__next__ ()) the #print (iter_l.__next__ ()) the #print (Next (iter_l)) #next ()---->iter_l.__next__ () the  -  in  the  the  About  the  the  the  +  - #def Test (): the #yield 1Bayi #yield 2 the #Yield 3 the #g=test () - #print (' From function ', g) - #print (g.__next__ ()) the #print (g.__next__ ()) the  the #Three -dimensional expression the #name= ' Alex ' - #name= ' Linhaifeng ' the #res= ' SB ' if name = = ' Alex ' Else ' handsome ' the #Print (res) the 94  the #List Parsing the #egg_list=[] the #For I in Range (Ten):98 #egg_list.append (' egg%s '%i) About #print (egg_list) - 101 #l=[' egg%s '%i for I in Range (Ten)]102 #l1=[' egg%s '%i for I in range (Ten) if I > 5]103 #ll = [' Egg '%s '%i for I in Range (Ten)]104 #lll = [' egg%s '%i for I in range (Ten) if I<5] the ## l1=[' egg%s '%i for I in range (Ten) if I > 5 else i] #没有四元表达式106 #l2=[' egg%s '%i for I in range (Ten) if I < 5] #没有四元表达式107 #108 ## Print (l)109 #print (L1) the #print (L2)111  the #laomuji= (' egg%s '%i for I in range) #生成器表达式113 #Laomuji = (' Dog egg%s '%i for I in Range ()) the #print (Laomuji) the # the #print (laomuji.__next__ ())117 #print (laomuji.__next__ ())118 #print (Next (Laomuji))119 #print (Next (Laomuji)) - #print (Next (Laomuji))121 #print (Next (Laomuji))122 #print (Next (Laomuji))123 #print (Next (Laomuji))124 #print (Next (Laomuji)) the #print (Next (Laomuji))126 127 #l=[1,2,3,34] - #map (func,l)129  the #Print (SUM (l))131 #print (sum ()) the #Print (SUM (i-I in range (10000)))133 Print(SUM (i forIinchRange (101)))
View Code

Python iterators and generators

Related Article

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.