zoukankan      html  css  js  c++  java
  • 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()

  • 相关阅读:
    Linux-1-用户管理
    接口相关资料整理
    JPype1使用总结
    1.django项目的创建(在CMD中)
    公有云、私有云和混合云的区别
    接口测试1.测试用例要点与模板
    PostMan Test 的脚本scripts编写方法
    Tensorflow RNN中的坑
    2019年终总结
    tensorflow中一种融合多个模型的方法
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501252.html
Copyright © 2011-2022 走看看