Python enumerate() 函数如何实现索引功能

 更新时间:2020年06月29日 11:09:06   作者:杨枫哥  
这篇文章主要介绍了Python enumerate() 函数如何实现索引功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.描述:

enumerate()函数用于将一个可遍历的数据对象(如列表,元组,字符串)组合为一个索引序列,同时列出数据和数据索引(下标),一般用于for循环当中

2.语法

enumerate(sequence, [start=0])

3.参数:

  • sequence:一个序列,迭代器或其他支持迭代对象
  • start:可选参数,下标起始位置,默认从索引0开始

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 = 10
index1 = 1, item1 = 20
index1 = 2, item1 = 30
index1 = 3, item1 = 40
index1 = 4, item1 = maple
index1 = 5, item1 = yf
index1 = 6, item1 = 60
------------------------------
index2 = 2, item2 = 10
index2 = 3, item2 = 20
index2 = 4, item2 = 30
index2 = 5, item2 = 40
index2 = 6, item2 = maple
index2 = 7, item2 = yf
index2 = 8, item2 = 60
******************************
index3 = 0, item3 = 100
index3 = 1, item3 = 200
index3 = 2, item3 = 300
index3 = 3, item3 = 400
index3 = 4, item3 = hao
index3 = 5, item3 = qazert
index3 = 6, item3 = 600
==============================
index4 = 4, item4 = 100
index4 = 5, item4 = 200
index4 = 6, item4 = 300
index4 = 7, item4 = 400
index4 = 8, item4 = hao
index4 = 9, item4 = qazert
index4 = 10, item4 = 600
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
index4 = 0, item4 = 1
index4 = 1, item4 = 2
index4 = 2, item4 = 3
index4 = 3, item4 = 4
index4 = 4, item4 = q
index4 = 5, item4 = w
index4 = 6, item4 = e
index4 = 7, item4 = r
index4 = 8, item4 = t
index4 = 9, item4 = j
index4 = 10, item4 = d
index4 = 11, item4 = s
index4 = 12, item4 = a
index4 = 13, item4 = 2
index4 = 14, item4 = 2
index4 = 15, item4 = 3
index4 = 16, item4 = 2
index4 = 17, item4 = 3
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
index4 = 6, item4 = 1
index4 = 7, item4 = 2
index4 = 8, item4 = 3
index4 = 9, item4 = 4
index4 = 10, item4 = q
index4 = 11, item4 = w
index4 = 12, item4 = e
index4 = 13, item4 = r
index4 = 14, item4 = t
index4 = 15, item4 = j
index4 = 16, item4 = d
index4 = 17, item4 = s
index4 = 18, item4 = a
index4 = 19, item4 = 2
index4 = 20, item4 = 2
index4 = 21, item4 = 3
index4 = 22, item4 = 2
index4 = 23, item4 = 3

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python中的单下划线和双下划线使用场景详解

    Python中的单下划线和双下划线使用场景详解

    这篇文章主要介绍了Python中的单下划线和双下划线使用场景详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • 关于Python排序问题(冒泡/选择/插入)

    关于Python排序问题(冒泡/选择/插入)

    这篇文章主要介绍了关于Python排序问题(冒泡/选择/插入),学过C语言肯定接触过排序问题,我们最常用的也就是冒泡排序、选择排序、插入排序,需要的朋友可以参考下
    2023-04-04
  • Python运算符+与+=的方法实例

    Python运算符+与+=的方法实例

    这篇文章主要介绍了Python运算符+与+=的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python操作qml对象过程详解

    Python操作qml对象过程详解

    这篇文章主要介绍了Python操作qml对象过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • 详解Python匹配多行文本块的正则表达式

    详解Python匹配多行文本块的正则表达式

    这篇文章主要介绍了Python 匹配多行文本块的正则表达式,该解决方案折衷了已知和未知模式的几种方法,并解释了匹配模式的工作原理,本文给大家介绍的非常详细,需要的朋友可以参考下
    2023-06-06
  • 利用python程序生成word和PDF文档的方法

    利用python程序生成word和PDF文档的方法

    这篇文章主要给大家介绍了利用python程序生成word和PDF文档的方法,文中给出了详细的介绍和示例代码,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
    2017-02-02
  • python3 dict ndarray 存成json,并保留原数据精度的实例

    python3 dict ndarray 存成json,并保留原数据精度的实例

    今天小编就为大家分享一篇python3 dict ndarray 存成json,并保留原数据精度的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • python 列表元素左右循环移动 的多种解决方案

    python 列表元素左右循环移动 的多种解决方案

    这篇文章主要介绍了python 列表元素左右循环移动 的多种解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Ubuntu+python将nii图像保存成png格式

    Ubuntu+python将nii图像保存成png格式

    这篇文章主要介绍了Ubuntu+python将nii图像保存成png格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • 一篇文章带你学习Python3的高级特性(1)

    一篇文章带你学习Python3的高级特性(1)

    这篇文章主要为大家详细介绍了Python3的高阶函数,主要介绍什么是高级特性,高级特性的用法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论