个人商城网站制作费用,中小企业查询,牡丹江3d网站开发,小程序定制公司哪家好enumerate函数用于遍历序列中的元素以及它们的下标,多用于在for循环中得到计数,enumerate参数为可遍历的变量#xff0c;如 字符串#xff0c;列表等一般情况下对一个列表或数组既要遍历索引又要遍历元素时#xff0c;会这样写#xff1a;for i in range (0,len(list)):pri…enumerate函数用于遍历序列中的元素以及它们的下标,多用于在for循环中得到计数,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代码实例1i 0seq [one, two, three]for element in seq:print i, seq[i]i 10 one1 two2 three代码实例2seq [one, two, three]for i, element in enumerate(seq):print i, seq[i]0 one1 two2 three代码实例3for i,j in enumerate(abc):print i,j0 a1 b2 c