Python学习笔记 - 生成器generator
2015-06-29 22:24
941 查看
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # generator 生成器 L = [x * x for x in range(10)] print(L) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] g = (x * x for x in range(10)) print(g) # <generator object <genexpr> at 0x00000000028F0120> print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) print(next(g)) ''' 0 1 4 9 16 25 36 49 64 81 ''' # print(next(g)) ''' Traceback (most recent call last): File "E:\Code\python\learn\generator.py", line 23, in <module> print(next(g)) StopIteration ''' # 上面这种调用方法太变态了 # 正确的使用for循环 g = (x * x for x in range(10)) for n in g: print(n) ''' 0 1 4 9 16 25 36 49 64 81 ''' def fib(max): n, a, b = 0, 0, 1 while n < max: print(b) a, b = b, a + b # 注意这里 n = n + 1 return 'done' fib(6) ''' 1 1 2 3 5 8 'done' ''' def fib(max): n, a, b = 0, 0, 1 while n < max: yield b a, b = b, a + b n = n + 1 return 'done' # 如果一个函数定义中包含yield关键字,那么这个函数就不再是一个普通函数 # 而是一个generator f = fib(6) f # <generator object fib at 0x00000000022011F8> def odd(): print('step 1') yield 1 print('step 2') yield(3) print('step 3') yield(5) ''' >>> o = odd() >>> next(o) step 1 1 >>> next(o) step 2 3 >>> next(o) step 3 5 ''' for n in fib(6): print(n) # 练习 - 杨辉三角 def triangles(): a = [1]; while True: yield a a = [sum(i) for i in zip([0] + a, a + [0])] n = 0 for t in triangles(): print(t) n = n + 1 if n == 10: break ''' [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] [1, 5, 10, 10, 5, 1] [1, 6, 15, 20, 15, 6, 1] [1, 7, 21, 35, 35, 21, 7, 1] [1, 8, 28, 56, 70, 56, 28, 8, 1] [1, 9, 36, 84, 126, 126, 84, 36, 9, 1] '''
相关文章推荐
- Python学习笔记 - 生成器generator
- 列表元素的个数及元素的值可改变;元组值不可以改变-《Python核心编程》P26
- Python学习笔记 - 列表生成式listComprehensions
- Python学习笔记 - 列表生成式listComprehensions
- Python学习笔记 - 迭代Iteration
- Python学习笔记 - 迭代Iteration
- Python While循环语句
- python时间与字符串之间的转换
- python3中关于编码那些事儿
- Python学习笔记 - 切片
- Python学习笔记 - 切片
- python初始---程序一(login登陆)
- 网易新闻评论爬虫(python+selenium+PhantomJS)
- Python 浅拷贝和深拷贝
- Python collections
- Python学习笔记(10):面向对象编程
- python实现数字滤波器 IIR FIR
- 用python作个简单文件拷贝操作
- Python重定向到文件
- Python数值计算工具包numpy,scipy,theano等的安装--Anaconda