python中迭代器(iterator)用法实例分析


Posted in Python onApril 29, 2015

本文实例讲述了python中迭代器(iterator)用法。分享给大家供大家参考。具体如下:

#---------------------------------------
#      Name: iterators.py
#     Author: Kevin Harris
# Last Modified: 03/11/04
# Description: This Python script demonstrates how to use iterators.
#---------------------------------------
myTuple = (1, 2, 3, 4)
myIterator = iter( myTuple )
print( next( myIterator ) )
print( next( myIterator ) )
print( next( myIterator ) )
print( next( myIterator ) )
# Becareful, one more call to next() 
# and this script will throw an exception!
#print myIterator.next() 
print( " " )
#---------------------------------------
# If you have no idea how many items 
# can be safely accesd via the iterator,
# use a try/except block to keep your script from crashing.
myTuple2 = ( "one", "two", "three", "four" )
myIterator2 = iter( myTuple2 )
while 1:
  try:
    print( next( myIterator2 ) )
  except StopIteration:
    print( "Exception caught! Iterator must be empty!" )
    break
input( '\n\nPress Enter to exit...' )

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python中正则表达式的详细教程
Apr 30 Python
python实现井字棋游戏
Mar 30 Python
Python数据类型详解(三)元祖:tuple
May 08 Python
请不要重复犯我在学习Python和Linux系统上的错误
Dec 12 Python
Python中%是什么意思?python中百分号如何使用?
Mar 20 Python
python 对dataframe下面的值进行大规模赋值方法
Jun 09 Python
numpy中的ndarray方法和属性详解
May 27 Python
详解Python利用random生成一个列表内的随机数
Aug 21 Python
pymysql 插入数据 转义处理方式
Mar 02 Python
Python基于smtplib协议实现发送邮件
Jun 03 Python
python openpyxl模块的使用详解
Feb 25 Python
Python opencv缺陷检测的实现及问题解决
Apr 24 Python
在Python中使用HTMLParser解析HTML的教程
Apr 29 #Python
python安装以及IDE的配置教程
Apr 29 #Python
python获取从命令行输入数字的方法
Apr 29 #Python
在Python中处理XML的教程
Apr 29 #Python
python搜索指定目录的方法
Apr 29 #Python
python中sleep函数用法实例分析
Apr 29 #Python
介绍Python中内置的itertools模块
Apr 29 #Python
You might like
透析PHP的配置文件php.ini
2006/10/09 PHP
PHP脚本中include文件出错解决方法
2008/11/20 PHP
PHPThumb图片处理实例
2014/05/03 PHP
PHP字符串比较函数strcmp()和strcasecmp()使用总结
2014/11/19 PHP
Yii2搭建后台并实现rbac权限控制完整实例教程
2016/04/28 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
PHP基于PDO扩展操作mysql数据库示例
2018/12/24 PHP
PHP Ajax跨域问题解决方案代码实例
2020/08/01 PHP
javascript 实用的文字链提示框效果
2010/06/30 Javascript
script标签的 charset 属性使用说明
2010/12/04 Javascript
IE6下CSS图片缓存问题解决方法
2010/12/09 Javascript
基于jQuery 实现bootstrapValidator下的全局验证
2015/12/07 Javascript
jQuery中deferred对象使用方法详解
2016/07/14 Javascript
jquery代码规范让代码越来越好看
2017/02/03 Javascript
深入理解Angularjs中$http.post与$.post
2017/05/19 Javascript
React中如何引入Angular组件详解
2018/08/09 Javascript
微信小程序把百度地图坐标转换成腾讯地图坐标过程详解
2019/07/10 Javascript
微信小程序可滑动月日历组件使用详解
2019/10/21 Javascript
Vue路由对象属性 .meta $route.matched详解
2019/11/04 Javascript
vue2.0 获取从http接口中获取数据,组件开发,路由配置方式
2019/11/04 Javascript
vue.config.js常用配置详解
2019/11/14 Javascript
Python写的Tkinter程序屏幕居中方法
2015/03/10 Python
python and or用法详解
2019/06/26 Python
Django实现web端tailf日志文件功能及实例详解
2019/07/28 Python
PYTHON实现SIGN签名的过程解析
2019/10/28 Python
使用Tensorflow实现可视化中间层和卷积层
2020/01/24 Python
python实现按键精灵找色点击功能教程,使用pywin32和Pillow库
2020/06/04 Python
python Matplotlib数据可视化(2):详解三大容器对象与常用设置
2020/09/30 Python
CSS3教程:新增加的结构伪类
2009/04/02 HTML / CSS
ProBikeKit美国官网:自行车套件,跑步和铁人三项套件
2016/10/13 全球购物
在线学习西班牙语、法语或其他语言:Babbel.com
2018/02/07 全球购物
BookOutlet加拿大:在网上书店购买廉价折扣图书和小说
2018/10/05 全球购物
中班中秋节活动反思
2014/02/18 职场文书
岗位廉洁从业承诺书
2014/03/28 职场文书
经理聘任证明
2015/03/02 职场文书
2016年质量月活动总结报告
2016/04/05 职场文书