时间:2021-05-23
1.描述:
enumerate()函数用于将一个可遍历的数据对象(如列表,元组,字符串)组合为一个索引序列,同时列出数据和数据索引(下标),一般用于for循环当中
2.语法
enumerate(sequence, [start=0])
3.参数:
4.返回值
返回enumerate(枚举)对象
5.实例
list1 = [10,20,30,40,"maple","yf",60]tup1 = (100,200,300,400,"hao","qazert",600)str1 = "1234qwertjdsa22323"for index1,item1 in enumerate(list1): print("index1 = %d, item1 = %s" %(index1,item1,))print("------------------------------")for index2, item2 in enumerate(list1,start = 2): print("index2 = %d, item2 = %s" %(index2,item2,))print("******************************")for index3,item3 in enumerate(tup1): print("index3 = %d, item3 = %s" % (index3, item3,))print("==============================")for index4,item4 in enumerate(tup1, start = 4): print("index4 = %d, item4 = %s" % (index4, item4,))print("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")for index5,item5 in enumerate(str1): print("index4 = %d, item4 = %s" % (index5, item5,))print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")for index6,item6 in enumerate(str1,start = 6): print("index4 = %d, item4 = %s" % (index6, item6,)) #输出的结果如下:index1 = 0, item1 = 10index1 = 1, item1 = 20index1 = 2, item1 = 30index1 = 3, item1 = 40index1 = 4, item1 = mapleindex1 = 5, item1 = yfindex1 = 6, item1 = 60------------------------------index2 = 2, item2 = 10index2 = 3, item2 = 20index2 = 4, item2 = 30index2 = 5, item2 = 40index2 = 6, item2 = mapleindex2 = 7, item2 = yfindex2 = 8, item2 = 60******************************index3 = 0, item3 = 100index3 = 1, item3 = 200index3 = 2, item3 = 300index3 = 3, item3 = 400index3 = 4, item3 = haoindex3 = 5, item3 = qazertindex3 = 6, item3 = 600==============================index4 = 4, item4 = 100index4 = 5, item4 = 200index4 = 6, item4 = 300index4 = 7, item4 = 400index4 = 8, item4 = haoindex4 = 9, item4 = qazertindex4 = 10, item4 = 600%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%index4 = 0, item4 = 1index4 = 1, item4 = 2index4 = 2, item4 = 3index4 = 3, item4 = 4index4 = 4, item4 = qindex4 = 5, item4 = windex4 = 6, item4 = eindex4 = 7, item4 = rindex4 = 8, item4 = tindex4 = 9, item4 = jindex4 = 10, item4 = dindex4 = 11, item4 = sindex4 = 12, item4 = aindex4 = 13, item4 = 2index4 = 14, item4 = 2index4 = 15, item4 = 3index4 = 16, item4 = 2index4 = 17, item4 = 3$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$index4 = 6, item4 = 1index4 = 7, item4 = 2index4 = 8, item4 = 3index4 = 9, item4 = 4index4 = 10, item4 = qindex4 = 11, item4 = windex4 = 12, item4 = eindex4 = 13, item4 = rindex4 = 14, item4 = tindex4 = 15, item4 = jindex4 = 16, item4 = dindex4 = 17, item4 = sindex4 = 18, item4 = aindex4 = 19, item4 = 2index4 = 20, item4 = 2index4 = 21, item4 = 3index4 = 22, item4 = 2index4 = 23, item4 = 3以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
enumerate函数用于遍历序列中的元素以及它们的下标。enumerate函数说明:enumerate()是python的内置函数enumerate在字典上是
enumerate函数enumerate是一个Python内置函数,一个功能强大的内置函数。其实功能强大不足以形容它,但是很难用一个词来形容它的用途。让我们来看
enumerate函数用于遍历序列中的元素以及它们的下标。enumerate函数说明:函数原型:enumerate(sequence,[start=0])功能:
enumerate函数用于遍历序列中的元素以及它们的下标。enumerate函数说明:函数原型:enumerate(sequence,[start=0])功能:
这个是python的一个内建函数,看书的时候发现了他,mark一下当我们既需要遍历索引同时需要遍历元素的时候,可以考虑使用enumerate函数,enumera