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


Posted in Python onJune 29, 2020

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的Django框架中的中间件
Jul 24 Python
如何利用Fabric自动化你的任务
Oct 20 Python
Python和Java进行DES加密和解密的实例
Jan 09 Python
Python使用functools实现注解同步方法
Feb 06 Python
安装python时MySQLdb报错的问题描述及解决方法
Mar 20 Python
python中实现将多个print输出合成一个数组
Apr 19 Python
matplotlib subplots 调整子图间矩的实例
May 25 Python
对python判断是否回文数的实例详解
Feb 08 Python
浅谈python编译pyc工程--导包问题解决
Mar 20 Python
Python图像处理模块ndimage用法实例分析
Sep 05 Python
python实现简单成绩录入系统
Sep 19 Python
将python文件打包exe独立运行程序方法详解
Feb 12 Python
解决Keras中CNN输入维度报错问题
Jun 29 #Python
Python字符串split及rsplit方法原理详解
Jun 29 #Python
浅谈Keras参数 input_shape、input_dim和input_length用法
Jun 29 #Python
使用 prometheus python 库编写自定义指标的方法(完整代码)
Jun 29 #Python
使用keras时input_shape的维度表示问题说明
Jun 29 #Python
在Keras中CNN联合LSTM进行分类实例
Jun 29 #Python
使用keras实现BiLSTM+CNN+CRF文字标记NER
Jun 29 #Python
You might like
一篇入门的php Class 文章
2007/04/04 PHP
PhpStorm terminal无法输入命令的解决方法
2016/10/09 PHP
Laravel中为什么不使用blpop取队列详析
2018/08/01 PHP
php 比较获取两个数组相同和不同元素的例子(交集和差集)
2019/10/18 PHP
终于解决了IE8不支持数组的indexOf方法
2013/04/03 Javascript
顶部缓冲下拉菜单导航特效的JS代码
2013/08/27 Javascript
Js 导出table内容到Excel的简单实例
2013/11/19 Javascript
用jQuery toggleClass 实现鼠标移上变色
2014/05/14 Javascript
JS常用函数使用指南
2014/11/23 Javascript
js判断浏览器版本以及浏览器内核的方法
2015/01/20 Javascript
jQuery选择器源码解读(五):tokenize的解析过程
2015/03/31 Javascript
让图片跳跃起来  javascript图片轮播特效
2016/02/16 Javascript
Bootstrap中表单控件状态(验证状态)
2016/08/04 Javascript
node.js入门教程之querystring模块的使用方法
2017/02/27 Javascript
详解node.js中的npm和webpack配置方法
2018/01/21 Javascript
在vue中实现echarts随窗体变化
2020/07/27 Javascript
python unittest实现api自动化测试
2018/04/04 Python
Python 3.x基于Xml数据的Http请求方法
2018/12/28 Python
PyQt5 加载图片和文本文件的实例
2019/06/14 Python
Python中拆分字符串的操作方法
2019/07/23 Python
Python批量启动多线程代码实例
2020/02/18 Python
通过python连接Linux命令行代码实例
2020/02/18 Python
Python基于yield遍历多个可迭代对象
2020/03/12 Python
简单了解django处理跨域请求最佳解决方案
2020/03/25 Python
如何在Python对Excel进行读取
2020/06/04 Python
python自动化测试三部曲之unittest框架的实现
2020/10/07 Python
澳大利亚领先的内衣店:Bendon Lingerie澳大利亚
2020/05/15 全球购物
英国排名第一的冲浪店:Ann’s Cottage
2020/06/21 全球购物
文员个人的求职信范文
2013/09/26 职场文书
学前教育专业毕业生自荐信
2013/10/03 职场文书
资金主管岗位职责范本
2014/03/04 职场文书
测绘工程专业求职信
2014/07/15 职场文书
大学生年度个人总结
2015/02/15 职场文书
先进基层党组织主要事迹材料
2015/11/03 职场文书
python - asyncio异步编程
2021/04/06 Python
Nginx使用ngx_http_upstream_module实现负载均衡功能示例
2022/08/05 Servers