Python设计模式(十六)【迭代器模式】
来源:互联网 发布:linux系统入门教程 编辑:程序博客网 时间:2024/06/05 18:43
人只要不失去方向,就不会失去自己!
"""Implementation of the iterator pattern with a generator一个生成器实现迭代器模式"""from __future__ import print_functiondef count_to(count): """由数字编号计数,最多五个""" numbers = ["one", "two", "three", "four", "five"] # 枚举()返回一个包含计数的元组(默认从0开始)从遍历序列获得值 for pos, number in zip(range(count), numbers): yield number# 测试生成器count_to_two = lambda: count_to(2)count_to_five = lambda: count_to(5)print('数到2...')for number in count_to_two(): print(number, end=' ')print()print('数到5...')for number in count_to_five(): print(number, end=' ')print()
运行结果如图:
0 0
- Python设计模式(十六)【迭代器模式】
- 设计模式C++实现十六:迭代器模式
- 设计模式(十六)迭代器模式
- Java设计模式《十六》迭代器模式
- 设计模式—迭代器模式(十六)
- 设计模式十六(职责链模式,python实现)
- 设计模式(十六)---观察者模式
- C++设计模式之十六:Iterator 迭代器
- 设计模式笔记(十六) —— 迭代器模式
- 设计模式(十六)----- 迭代器模式(Iterator)----(JAVA版)
- 设计模式实现(十六)---迭代器模式(Iterator)
- 设计模式之十六:迭代器模式(Iterator)
- 十六、迭代器模式——设计模式学习笔记
- 设计模式(十六)——迭代器模式
- 【设计模式】之十六Facede
- 设计模式(十六)之Command
- 十六、 中介者设计模式
- 设计模式(十六)state
- iOS--UILabel字体默认宽度和高度
- 改进Bootstrap中的响应式侧边栏
- tslib库移植selected device is not a touchscreen I understand
- CNN卷积神经网络--反向传播(3,初识卷积反向算法)
- SharePoint安装环境配置
- Python设计模式(十六)【迭代器模式】
- 透明通道图纹理贴图渐变色效果
- iOS 语音合成,语音播报功能(系统)
- c&c++ error
- composer
- phpexcel来做表格导出(多个工作sheet)
- 单目、二目、三目运算符
- jQuery学习笔记
- java线程安全问题之静态成员变量、实例成员变量、局部变量