国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2019-11-25 17:36:45
字體:
來源:轉載
供稿:網友

本文實例講述了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!" )    breakinput( '/n/nPress Enter to exit...' )

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平江县| 砀山县| 新昌县| 绥中县| 邵阳县| 仙桃市| 丹东市| 莱芜市| 沙湾县| 建湖县| 恩平市| 稻城县| 库车县| 惠水县| 大埔区| 江孜县| 台江县| 梁平县| 六安市| 麻栗坡县| 阿巴嘎旗| 正镶白旗| 阳春市| 崇礼县| 河东区| 大方县| 西丰县| 会泽县| 晋城| 泌阳县| 禄劝| 乌兰察布市| 黎川县| 阿巴嘎旗| 昌图县| 两当县| 公安县| 阿拉尔市| 房山区| 内丘县| 全南县|