Working with Generators of Python__Python

來源:互聯網
上載者:User
#enumerate#seasons=['spring','summer','autumn','winter']seasons=('spring','summer','autumn','winter') # env tuple is works ,but after enumerate it will be listprint seasonsprint list(enumerate(seasons)) #[(0, 'spring'), (1, 'summer'), (2, 'autumn'), (3, 'winter')]print list(enumerate(seasons,start=1)) #[(1, 'spring'), (2, 'summer'), (3, 'autumn'), (4, 'winter')]# Eval Function   why  ????  what can i do with funx =1print eval('x==1')   # Trueprint eval('x + 1')print x +1#iter functionswith open('myfile.txt') as fh:    for line in iter(fh.readline,''):  # fh.readline() not works here        print linefh2 = open("myfile.txt")       # for personal  I like this one ,it much mor clearfor line in fh2:  # it is works    print line#Nested Sequencesplain =['spring','autumn','summer','winter']print plain[1][1]   # usea = [('alxs','home')]print seaprint sea[0][0]  #alxsprint sea[0][0][0]   # got a#generator functionsdef main():    print 'this is a smaple generator function '    for i in range(25):        print (i)   # not got  25    print inclusive_range(0,25,1) #<generator object inclusive_range at 0x00000000024D0510>    for i in inclusive_range(0,25,1): # this function has no connection with function range        print i    print '---------------------------------------'    #o =inclusive_ranges()   #TypeError: requires at least 1 param    #one =inclusive_ranges(6)    #two =inclusive_ranges(1,4)    #three =inclusive_ranges(2,7,2)    four =inclusive_ranges(1,4,6,8)   #TypeError: expected at most 3 param received 4    for i in four :print idef inclusive_range(start,stop,step):    i = start    while i <=stop:        yield  i        #print i+100        i+=step#using Gnerators with classesclass inclusive_ranges:    def __init__(self,*args):        numargs =len(args)        if numargs < 1 : raise TypeError('requires at least 1 param')        elif numargs == 1 :            self.stop=args[0]            self.start = 0            self.step =1        elif numargs ==2:            self.step = 1            self.start = args[0]            self.stop =args[1]            #(self.start,self.stop) =args        elif numargs ==3:            (self.start,self.stop,self.step)=args        else:raise  TypeError('expected at most 3 param received {}' .format(numargs))    def __iter__(self):        i =self.start        while i <= self.stop:            yield  i            i += self.stepmain()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.