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文件和目录操作函数小结
Jul 11 Python
详解Python中映射类型(字典)操作符的概念和使用
Aug 19 Python
Python 登录网站详解及实例
Apr 11 Python
Python自定义函数定义,参数,调用代码解析
Dec 27 Python
CentOS7下python3.7.0安装教程
Jul 30 Python
python实现自动获取IP并发送到邮箱
Dec 26 Python
Django模板Templates使用方法详解
Jul 19 Python
用python求一重积分和二重积分的例子
Dec 06 Python
python求前n个阶乘的和实例
Apr 02 Python
详解python tkinter 图片插入问题
Sep 03 Python
Python 操作SQLite数据库的示例
Oct 16 Python
Python实现日志实时监测的示例详解
Apr 06 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提取视频网站页面中的FLASH地址的代码
2010/04/17 PHP
修改php.ini不生效问题解决方法(上传大于8M的文件)
2013/06/14 PHP
Zend Framework教程之Resource Autoloading用法实例
2016/03/08 PHP
javascript的对话框详解与参数
2007/03/08 Javascript
Prototype中dom对象方法汇总
2008/09/17 Javascript
js动态控制table的tr、td增加及删除的具体实现
2014/04/30 Javascript
控制文字内容的显示与隐藏示例
2014/06/11 Javascript
jQuery判断多个input file 都不能为空的例子
2015/06/23 Javascript
javascript制作照片墙及制作过程中出现的问题
2016/04/04 Javascript
webpack+vue.js快速入门教程
2016/10/12 Javascript
详解VueJs异步动态加载块
2017/03/09 Javascript
jQuery动态添加元素无法触发绑定事件的解决方法分析
2018/01/02 jQuery
cropper js基于vue的图片裁剪上传功能的实现代码
2018/03/01 Javascript
vue实现pdf导出解决生成canvas模糊等问题(推荐)
2018/10/18 Javascript
jquery实现二级导航下拉菜单效果实例
2019/05/14 jQuery
webpack 代码分离优化快速指北
2019/05/18 Javascript
[01:01:35]Optic vs paiN 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
[20:39]DOTA2-DPC中国联赛 正赛开幕式 1月18日
2021/03/11 DOTA
用python写的一个wordpress的采集程序
2016/02/27 Python
Python面向对象class类属性及子类用法分析
2018/02/02 Python
利用Python写一个爬妹子的爬虫
2018/06/08 Python
Pycharm+Python+PyQt5使用详解
2019/09/25 Python
Django choices下拉列表绑定实例
2020/03/13 Python
Python自省及反射原理实例详解
2020/07/06 Python
PyCharm+PyQt5+QtDesigner配置详解
2020/08/12 Python
如何从csv文件构建Tensorflow的数据集
2020/09/21 Python
床上用品全球在线购物:BeddingInn
2016/12/18 全球购物
寻找迷宫的一条出路,o通路;X:障碍
2016/07/10 面试题
C#笔试题集合
2013/06/21 面试题
StringBuilder和String的区别
2015/05/18 面试题
客户代表自我评价范例
2013/09/24 职场文书
研究生导师推荐信
2014/09/06 职场文书
2014年文明创建工作总结
2014/11/25 职场文书
推销搭讪开场白
2015/05/28 职场文书
中国古代史学名著《战国策》概述
2019/08/09 职场文书
python3使用diagrams绘制架构图的步骤
2021/04/08 Python