• 6.3 迭代 递归


    迭代器

    count = 0
    while True:
       if count ==101:
           break
       print(count)
       count += 1
       

    python中一切皆对象

    height = 180
    salary = 3.2
    name = 'nick'
    hobby_list1 = ['run','read']
    hobby_tup = ('run','read')
    info_dict1 = {'name':'nick','weight':140}
    hobby_set = {'read','run','run'}

    def func():
       pass

    # w和a模式都会自动生成文件
    f = open('test.txt','w',encoding='utf8')

    可迭代对象:只要拥有iter方法的对象就是可迭代对象

    height.__iter__
    salary.__iter__
    func.__iter__
    name.__iter__()
    hobby_list.__iter__()
    hobby_set.__iter__()
    hobby_tup.__iter__()
    info_dict.__iter__()
    f.__iter__()

    字符串、列表、元祖、字典、集合、文件都是可迭代对象

    hobby_list = ['run','read']
    hobby_list_iter = hobby_list.__iter__()  # 把列表变成可迭代对象
    # print(hobby_list_iter.__next__())
    # print(hobby_list_iter.__next__())
    # print(hobby_list_iter.__next__())

    # for k in info_dict:
    #     print(k)
    info_dict = {'name':'nick','weight':140}
    info_dict_iter = info_dict.__iter__()
    # print(info_dict_iter.__next__())
    # print(info_dict_iter.__next__())
    # print(info_dict_iter.__next__())

    next其实是在遍历和迭代对象的元素,一旦遍历完报错

    迭代器对象:拥有 iter _ 方法,可迭代对象拥有next_方法的才是迭代器对象,文件本身就是迭代器对象

    hobby_list2 = ['run','read']
    # count = 0
    # while True:
    #     print(hobby_list2[count])
    #
    #     count += 1
    #
    #     if count == len(hobby_list2):
    #         break

    # for循环不依赖索引取值

    # 这一段代码如果用c写,就是for循环的原理
    hobby_list2 = ['run','read']
    hobby_list2_iter = hobby_list2.__iter__()
    while True:
       try:
           print(hobby_list2_iter.__next__())
       except:
           break

    for i in hobby_list2:  # hobby_list2,把hobby_list2转化为可迭代对象
       print(i)



    print(hobby_list2) # 一筐鸡蛋
    print(hobby_list2.__iter__()) # 相比较列表,它节省内存空间,老母鸡

    print(hobby_list2_iter)
    print(hobby_list2_iter.__iter__().__iter__().__iter__())  # 迭代器对象使用iter方法后是迭代器对象本身

    可迭代对象:拥有iter方法的对象就是可迭代对象,推到:可迭代对象不一定是迭代器对象

    迭代器对象:拥有iter方法的和next方法的就是迭代器对象,推导:迭代器对象一定是可迭代对象

    文件即可迭代对象,又是迭代器对象

     

    三元表达式(三目表达式)

    dog_name = 'xiaogou'

    if dog_name =='fenggou':
       print('远离他')
    else:
       print('盘他')
       
    #不推荐使用
    print('远离他') if dog_name =='fenggou' else print('盘他')

    # 列表推导式
    # lis = []
    #
    # for i in range(100):
    #     lis.append(i)
    #
    # print(lis)

    # lis = [i*2 for i in range(100)]
    # print(lis)
    # 千万不要写这样的东西,否则真的会被骂傻逼
    lis1 = [i * 2 if i > 50 else i for i in range(100)]
    print(lis1)

    字典生产式


    lis = [i for i in range(10)]
    print(lis)

    dic2= dict.fromkeys([1,2,3,4],2)
    print(dic2)
    dic = {i:i**2 for i in range(10)}
    for i in dic.items():
       print(i)

    拉链函数
    res = zip('abcd',[1,2,3,4])
    dic = dict()
    for k,v in res:
       dic[k] = v

    print(dic)

    print({k:v for k,v in zip('abcd',[1,2,3,4])})

    递归

    def f2():
       print('from f2')
       
    # 递归: 函数掉函数自己,类似于循环,但是这个循环必须有结束条件
    import time
    def f1(x): # x=0
       print(x) # 0 1
       # time.sleep(1)
       x += 1 # 1 2
       if x ==101:
           return
       f1(x) # x=1
       
       
    def f2():
       f1()
    f1(0)

    def guess_age(age,count):
       age -= 2
       count -= 1
       if count ==1:
           print(age)
           return
       guess_age(age,count)
       
       
    guess_age(38,5)
    def guess_age(count): #age ==38 count =5
       #age -=2 #36.34.32.30
       count -= 1
       if count ==1:
           # print(age)
           return 26
       return guess_age(count) +2


    res = guess_age(5)
    print(res)
    def guess_age(count):# 5
       count -= 1# 4
       if count ==1:
           return 26
       return guess_age(count) +2# guess_age(4) + 2 # guess_age(3) + 2 # guess_age(2) + 2
    # guess_age(5) = guess_age(4) + 2 = (guess_age(3) + 2) + 2 = ((guess_age(2) + 2) + 2) + 2 = 26 + 2 + 2 + 2 = 32
    # 26 + 2 =guess_age(3)=28 # 28+2=guess_age(4) = 30 # 30 + 2
    res = guess_age(5)
    print(res)

     

  • 相关阅读:
    WIn7 磁盘分区工具试用记录
    DirectShow 开发环境搭建(整理)
    WinCE 在连续创建约 1000 个文件后,再创建文件失败。这是为什么???
    在命令行处理 console 应用执行的返回值
    WinCE 的发展史及相关基础知识
    DirectShow Filter 基础与简单的示例程序
    使用 VS2005 编译 directshow sample 时链接错误
    车载系统之 Windows CE 应用软件框架设计
    兰州烧饼
    对决
  • 原文地址:https://www.cnblogs.com/zrx19960128/p/10970137.html
Copyright © 2020-2023  润新知