Python's function (six) and recursive iterator

4.8 iterator

  1. Iterables

    # 迭代:  器:工具
    
    # 可迭代对象
    
    # list,dict,str,set,tuple -- 可迭代对象  使用灵活
    #迭代对象判断
    #方式一
    #对象具有__iter__()方法则为迭代对象
    
    #方式二
    #查看源码 CTRL+鼠标左键
    
    #方式三
    #print(dir(list)) 查看是否有__iter__()方法
    
    #官方声明只要具有__iter__()方法的就是可迭代对象
    
    #可迭代对象的优点:
    #    使用 灵活
    #    直接查看值
    #可迭代对象的缺点:
    #占位内存
    
    #取值方式:
    #list str tuple --索引
    #dict --键
    #set -- 直接取值
  2. Iterator

    #官方声明只要具有__iter__()方法__next__()方法就是迭代器
    f = open("xxxx","w")
    f.__iter__()
    f.__next__()
    
    lst=[1,2,3]
    new_lst=lst.__iter__()#将可迭代对象转换成迭代器
    new_lst.__iter__()
    new_lst.__next__()
  3. The nature of the for loop

    s="123"
    count=len(s)
    new_s=s.__iter__()
    while count:
     print(new_s.__next__())
     count-=1
    #结果为:3 2 1
    
    #for 循环真实本质
    s="123"
    new_s=s.__iter__()
    while True:
        try:
            print(new_s.__next__())
        except StopIteration:
            break
     #迭代器的优缺点:
    #优点:节省内存,惰性机制
    #缺点:使用不灵活,操作比较繁琐,不能直接查看值
  4. to sum up

    迭代器的特性:
     1、一次性的(用完就没了 )
     2、不能逆行(不能 后退)
     3、惰性机制(节省内存)
    什么是可迭代对象:
     具有很多私有方法,具有__iter__()方法就是一个可迭代对象
    什么是迭代器:
    具有__iter__()和__next__()方法就是迭代器
    
    迭代器什么时候使用:当容器数据比较多的时候使用迭代器

4.9 Recursion

  1. Profile: calls itself (and constantly calls itself) - an endless loop; a clear termination condition; meet the above two conditions are valid recursive

  2. Delivery: end condition has been met execution

  3. Return: From the end condition has been rolled back

  4. Official statement: maximum level of 1000 actual test 998/997 can be manually modified

  5. Recursive infinite loop

    def func()
     print(123)#2、输出123
     func()#3、不断调用func函数
    func()#1、调用func函数
  6. Recursive no return value

     def age(n): # 1,2,3
         if n == 3:
             return "猜对了"
         else:
             age(n+1)
     print(age(1))
    
     def age2(n):
         if n == 3:
             return "猜对了"
    
     def age1(n):
         if n == 3:
             return "猜对了"
         else:
             age2(n+1)
    
     def age(n):
         if n == 3:
             return "猜对了"
         else:
             age1(n+1)
     age(1)
    #当n=3时候有返回值 n=2 n=2时候无返回值所以是None

  7. Recursive return value

    # 1.宝元  18-2-2-2
    # 2.太白  18-2-2
    # 3.wusir 18-2
    # 4.alex  18
    
     def age(n):
         if n == 4:
             return 18
         else:
             return age(n+1)-2
     print(age(1))
    
    
     def age4(n):
         if n == 4:
             return 18
     def age3(n):
         if n == 4: # 问的是不是第四个了
             return 18
         else:
             return age4(n+1)-2
     def age2(n):
         if n == 4:  # 问的是不是第四个了
             return 18
         else:
             return age3(n+1)-2
     def age1(n):
         if n == 4: # 问的是不是第四个了
             return 18
         else:
             return age2(n+1)-2
     print(age1(1))
    #n=4 n=3 n=2 n=1 都有返回值 所以结果为18

Guess you like

Origin www.cnblogs.com/zhangdadayou/p/11415201.html