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利用beautifulSoup实现爬虫
Sep 29 Python
Windows和Linux下使用Python访问SqlServer的方法介绍
Mar 10 Python
Python编写电话薄实现增删改查功能
May 07 Python
python实现简单购物商城
May 21 Python
Python 3.x读写csv文件中数字的方法示例
Aug 29 Python
Python基于time模块求程序运行时间的方法
Sep 18 Python
Flask使用Pyecharts在单个页面展示多个图表的方法
Aug 05 Python
python tornado修改log输出方式
Nov 18 Python
Django Admin后台添加数据库视图过程解析
Apr 01 Python
Python钉钉报警及Zabbix集成钉钉报警的示例代码
Aug 17 Python
Python入门之基础语法详解
May 11 Python
Python实现天气查询软件
Jun 07 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开启gzip页面压缩实例代码
2010/03/11 PHP
深入理解PHP原理之异常机制
2010/08/21 PHP
PHP5函数小全(分享)
2013/06/06 PHP
laravel-admin的多级联动方法
2019/09/30 PHP
jQuery对象和DOM对象使用说明
2010/06/25 Javascript
详解javascript传统方法实现异步校验
2016/01/22 Javascript
Jquery 自定义事件实现发布/订阅的简单实例
2016/06/12 Javascript
Bootstrap图片轮播组件使用实例解析
2016/06/30 Javascript
JavaScript Ajax编程 应用篇
2016/07/02 Javascript
JavaScript跨域调用基于JSON的RESTful API
2016/07/09 Javascript
手机端 HTML5使用photoswipe.js仿微信朋友圈图片放大效果
2016/08/25 Javascript
w3c编程挑战_初级脚本算法实战篇
2017/06/23 Javascript
JavaScript hasOwnProperty() 函数实例详解
2017/08/04 Javascript
详解webpack性能优化——DLL
2017/10/20 Javascript
利用vue组件自定义v-model实现一个Tab组件方法示例
2017/12/06 Javascript
JavaScript实现封闭区域布尔运算的示例代码
2018/06/25 Javascript
微信小程序使用npm支持踩坑
2018/11/07 Javascript
vue-cli 3 全局过滤器的实例代码详解
2019/06/03 Javascript
[42:50]NB vs VP 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python opencv 图像尺寸变换方法
2018/04/02 Python
Django项目开发中cookies和session的常用操作分析
2018/07/03 Python
python实现n个数中选出m个数的方法
2018/11/13 Python
基于Python的PIL库学习详解
2019/05/10 Python
python下载库的步骤方法
2019/10/12 Python
基于Python实现船舶的MMSI的获取(推荐)
2019/10/21 Python
在HTML5中如何使用CSS建立不可选的文字
2014/10/17 HTML / CSS
Bonami斯洛伐克:购买家具和家居饰品
2019/07/02 全球购物
C和C++经典笔试题附答案解析
2014/08/18 面试题
直接有效的自我评价
2014/01/11 职场文书
实习鉴定评语
2014/01/19 职场文书
商业企业管理专业求职信
2014/07/10 职场文书
领导班子奢靡之风查摆问题及整改措施
2014/09/27 职场文书
赵乐秦在党的群众路线教育实践活动总结大会上的讲话稿
2014/10/25 职场文书
教师党员个人总结
2015/02/10 职场文书
幼儿园教师心得体会范文
2016/01/21 职场文书
如何使用PyCharm及常用配置详解
2021/06/03 Python