班级网站设计论文,公司网站实用性,网站建设规划文档,百度公司在哪本文实例讲述了python中enumerate函数用法。分享给大家供大家参考。具体分析如下#xff1a;今日发现一个新函数 enumerate 。一般情况下对一个列表或数组既要遍历索引又要遍历元素时#xff0c;会这样写#xff1a; for i in range (0,len(list)):print i ,list[i]但是这种…本文实例讲述了python中enumerate函数用法。分享给大家供大家参考。具体分析如下今日发现一个新函数 enumerate 。一般情况下对一个列表或数组既要遍历索引又要遍历元素时会这样写 for i in range (0,len(list)):print i ,list[i]但是这种方法有些累赘使用内置enumerrate函数会有更加直接优美的做法先看看enumerate的定义 def enumerate(collection):Generates an indexed series: (0,coll[0]), (1,coll[1]) ...i 0it iter(collection)while 1:yield (i, it.next())i 1enumerate会将数组或列表组成一个索引序列。使我们再获取索引和索引内容的时候更加方便如下 for indextext in enumerate(list)):print index ,text在cookbook里介绍如果你要计算文件的行数可以这样写 count len(open(thefilepath,rU).readlines())前面这种方法简单但是可能比较慢当文件比较大时甚至不能工作下面这种循环读取的方法更合适些。 Count -1For count,line in enumerate(open(thefilepath,rU))PassCount 1希望本文所述对大家的python程序设计有所帮助。