day25

一、迭代器

  1 """
  2 1 什么是迭代器
  3     迭代器就是用来迭代取值的工具
  4 
  5     什么是迭代?
  6         迭代就是一个重复的过程,但是每次重复都是基于上一次的结果进行的
  7 
  8         # 单纯的重复不叫迭代
  9         while True:
 10             1+1
 11 
 12         # 下面才是迭代的过程
 13         count = 1
 14         while count < 5:
 15             print(count)
 16             count += 1
 17 
 18 2、为何要用迭代器
 19     优点
 20     (1) 找到一种通用的迭代取值方案->for循环
 21     (2)惰性计算,节省内存
 22 
 23     缺点:
 24         1、不能取指定的值,只能往后next
 25         2、迭代器对象是一次性的:一个迭代器对象值取干净了,不能继续取了
 26 
 27 3、如何用迭代器
 28 
 29 """
 30 names = ['egon', 'tom', 'jack', 'lili', 'ttt']
 31 
 32 # i = 0
 33 # while i < len(names):
 34 #     print(names[i])
 35 #     i += 1
 36 
 37 
 38 # 可迭代对象
 39 #          1、内置有__iter__方法的对象都是可迭代对象
 40 "abc".__iter__()
 41 [1, 2, 3].__iter__()
 42 (1, 2, 3).__iter__()
 43 {"x": 1, "y": 2}.__iter__()
 44 {1, 2, 3, 4}.__iter__()
 45 # open('a.txt', mode='r').__iter__()
 46 # open('a.txt', mode='r').__next__()
 47 
 48 
 49 # 调用可迭代对象的__iter__()会得到一个返回值,该返回值是迭代器对象
 50 # 迭代对象对象的特点:
 51 #           1、内置有__next__()方法
 52 #           2、内置有__iter__()方法
 53 
 54 # s = "abc"
 55 # iter_s = s.__iter__()  # iter_s = iter(s)
 56 # print(iter_s.__next__())  # print(next(ites_s))
 57 # print(iter_s.__next__())
 58 # print(iter_s.__next__())
 59 # print(iter_s.__next__())
 60 
 61 
 62 # dic = {"x":1,"y":2}
 63 # iter_dic = dic.__iter__()
 64 # print(iter_dic.__next__())
 65 # print(iter_dic.__next__())
 66 
 67 
 68 # items = [111, 222, 333]
 69 
 70 # print(len(items))  # items.__len__()
 71 
 72 # iter_s = iter(items)
 73 #
 74 # print(iter_s.__iter__().__iter__().__iter__() is iter_s)
 75 
 76 
 77 # names = ['egon', 'tom', 'jack', 'lili', 'ttt']
 78 names = {"x":1,"y":2,'z':3}
 79 
 80 # iter_names = iter(names)
 81 # while True:
 82 #     try:
 83 #         print(next(iter_names))
 84 #     except StopIteration:
 85 #         break
 86 
 87 # for工作原理
 88 # 1、调用in后那个对象的__iter__方法,得到一个迭代器对象iter_names
 89 # 2、x=next(iter_names),然后运行循环体代码
 90 # 3、重复步骤2,直到取完值,抛出异常,for循环会帮我们捕捉异常结束循环
 91 # for x in names:
 92 #     print(x)
 93 
 94 
 95 names = ['egon', 'tom', 'jack', 'lili', 'ttt']
 96 
 97 
 98 # iter_s1 = iter(names)
 99 # print(next(iter_s1))
100 # print(next(iter_s1))
101 # print(next(iter_s1))
102 # print(next(iter_s1))
103 # print(next(iter_s1))
104 #
105 # iter_s2=iter(names)
106 #
107 # print(next(iter_s2))
108 # print(next(iter_s2))
109 # print(next(iter_s2))
110 # print(next(iter_s2))
111 # print(next(iter_s2))
112 #
113 
114 iter_names = iter(names)
115 for x in iter_names :
116     print(x)
117 
118 print('='*50)
119 
120 for x in iter_names :
121     print(x)

二、生成器

 1 # l=[1,2,3]
 2 # res=iter(l)
 3 # print(res)
 4 
 5 # yield 对比 return
 6 # 相同点:都可以返回任意类型、任意个数的值
 7 # 不同点:return只能返回值一次值,函数就立即结束
 8 #        yield可以返回多次值,yield可以暂停函数的运行
 9 # def func():
10 #     print('one...')
11 #     yield 111
12 #     print('two...')
13 #     yield 222
14 #     print('three...')
15 #     yield 333
16 #     print('fourth')
17 #
18 # # 函数内但凡出现yield关键字,再调用函数不会立即执行函数体代码,而会返回一个
19 # # 生成器对象,生成器对象本质就是一个自定义的迭代器对象
20 # g=func()
21 # # g.__iter__()
22 # # g.__next__()
23 # # print(g)
24 #
25 # res=next(g)
26 # print(res)
27 #
28 # res=next(g)
29 # print(res)
30 #
31 # res=next(g)
32 # print(res)
33 #
34 # next(g)
35 
36 
37 
38 
39 def func():
40     res = 0
41     while True:
42         yield res
43         res += 1
44 
45 g=func()
46 # print(next(g))
47 # print(next(g))
48 # print(next(g))
49 for i in g:
50     print(i)

三、函数的递归

  1 """
  2 1、什么是函数递归
  3      函数递归是函数嵌套调用的一种特殊格式
  4      即在调用一个函数时,在其内部又调用了自己
  5     # def foo():
  6     #     print('from foo')
  7     #     foo()
  8     #
  9     #
 10     # foo()
 11 
 12     import sys
 13     print(sys.getrecursionlimit())
 14     sys.setrecursionlimit(2000)
 15 
 16     递归应该分为两个阶段
 17     1、回溯:一层一层往下调用
 18     2、递推:一层一层向上推出结果
 19 
 20 
 21 # 2、为何要用函数递归
 22     函数递归提供了一种基于函数实现的新的循环机制
 23 
 24 
 25 
 26 # 3、如何用函数递归
 27 """
 28 
 29 # age(5)=age(4)+10
 30 # age(4)=age(3)+10
 31 # age(3)=age(2)+10
 32 # age(2)=age(1)+10
 33 # age(1)=18
 34 
 35 # n > 1 :age(n)=age(n-1)+10
 36 # n==1   :age(1)=18
 37 #
 38 # def age(n):
 39 #     if n == 1:
 40 #         return 18
 41 #     else:
 42 #         return age(n-1)+10
 43 #
 44 # res = age(5)
 45 # print(res)
 46 
 47 # 回溯
 48 # 第一次:n=5
 49 # age(5) = age(4) + 10
 50 
 51 # 第二次:n=4
 52 # age(4) = age(3) + 10
 53 
 54 # 第三次:n=3
 55 # age(3) = age(2) + 10
 56 
 57 # 第四次:n=2
 58 # age(2) = age(1) + 10
 59 
 60 # 第五次:n=1
 61 # age(1) = 18
 62 
 63 
 64 # 例1:
 65 # l = [11, [22, [33, [44, [55, [66, [77, [88, [99]]]]]]]]]
 66 #
 67 #
 68 # def func(l):
 69 #     for item in l:
 70 #         if type(item) is list:
 71 #             func(item)
 72 #         else:
 73 #             print(item)
 74 #
 75 # func(l)
 76 
 77 # 例2:递归实现二分法
 78 
 79 nums = [3, 7, 9, 13, 21, 33, 57, 63, 78, 99]
 80 
 81 
 82 
 83 def search(nums,find_num):
 84     print(nums)
 85     if len(nums) == 0:
 86         print('not exists')
 87         return
 88     mid = len(nums) // 2
 89     if find_num > nums[mid]:
 90         # 找右半部分
 91         search(nums[mid+1:],find_num)
 92 
 93     elif find_num < nums[mid]:
 94         # 找左半部分
 95         search(nums[:mid],find_num)
 96     else:
 97         print('find it')
 98 
 99 
100 # search(nums,63)
101 search(nums,64)

 

四、三元表达式

 1 def max2(x,y):
 2     if x > y:
 3         return x
 4     else:
 5         return y
 6 
 7 
 8 # 条件成立时的返回值 if 条件 else 条件不成立时的返回值
 9 
10 x = 10
11 y = 20
12 
13 # res = x if x > y else y
14 res = "ok" if 'a' != "a" else "no"
15 print(res)

 

五、生成式

 1 # 列表生成式
 2 # l = []
 3 # for i in range(10):
 4 #     if i > 3:
 5 #         l.append(i)
 6 
 7 # l = ["ok" for i in range(10) if i > 3]
 8 # print(l)
 9 
10 
11 # names = ['egon', "lxx_sb", "hxx_sb", "wxx_sb", "lxx_sb"]
12 # res = [name for name in names if name.endswith('sb')]
13 # print(res)
14 
15 
16 # names = ["lxx", "hxx", "wxx", "lxx"]
17 # print([name+"_sb" for name in names])
18 
19 
20 # 字典生成式
21 # print({"k%s" %i:i for i in range(3)})
22 
23 # 集合生成式
24 # print({i for i in range(3)})
25 
26 # 生成器表达式
27 # res = (i for i in range(5))
28 # print(res)
29 # print(next(res))
30 # print(next(res))
31 
32 
33 with open('a.txt', mode='rt', encoding='utf-8') as f:
34     # res = f.read()
35     # print(len(res))  # 23个字符
36 
37     # res = 0
38     # for line in f:
39     #     res += len(line)
40 
41     # res = sum((len(line) for line in f))
42     res = sum(len(line) for line in f)
43     print(res)

 

posted @ 2021-07-06 11:03  Gnomeshghy  阅读(35)  评论(0)    收藏  举报