• Working with Generators of Python


    #enumerate
    #seasons=['spring','summer','autumn','winter']
    seasons=('spring','summer','autumn','winter') # env tuple is works ,but after enumerate it will be list
    print seasons
    print 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 fun
    
    x =1
    print eval('x==1')   # True
    print eval('x + 1')
    print x +1
    
    #iter functions
    
    with open('myfile.txt') as fh:
        for line in iter(fh.readline,''):  # fh.readline() not works here
            print line
    
    fh2 = open("myfile.txt")       # for personal  I like this one ,it much mor clear
    for line in fh2:  # it is works
        print line
    
    
    
    #Nested Sequences
    
    plain =['spring','autumn','summer','winter']
    print plain[1][1]   # u
    
    sea = [('alxs','home')]
    print sea
    print sea[0][0]  #alxs
    print sea[0][0][0]   # got a
    
    
    #generator functions
    
    def 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 i
    
    def inclusive_range(start,stop,step):
        i = start
        while i <=stop:
            yield  i
            #print i+100
            i+=step
    
    #using Gnerators with classes
    
    class 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.step
    
    
    main()

  • 相关阅读:
    JavaScript 利用 filter+正则表达式对 list进行过滤包括模糊匹配
    odoo恢复数据库后,登录之后,显示空白页的问题
    Vue修改对象的属性值后页面不重新渲染问题
    智慧树(知到)刷课软件更新详情
    【C#】对两张图片进行矩阵运算会怎么样?
    DICOM协议中的UID详解
    DICOM医学文件的解析
    robots.txt协议——网络爬虫的“盗亦有道”
    机器学习算法之Kmeans算法(K均值算法)
    OPENGL shader 知识点
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501252.html
Copyright © 2020-2023  润新知