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中的类学习笔记
Sep 23 Python
实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
Jan 20 Python
Python 序列的方法总结
Oct 18 Python
python如何通过twisted实现数据库异步插入
Mar 20 Python
Pandas中把dataframe转成array的方法
Apr 13 Python
解决pip install的时候报错timed out的问题
Jun 12 Python
Python实现基于PIL和tesseract的验证码识别功能示例
Jul 11 Python
pandas使用apply多列生成一列数据的实例
Nov 28 Python
浅析python内置模块collections
Nov 15 Python
解决python web项目意外关闭,但占用端口的问题
Dec 17 Python
python3字符串输出常见面试题总结
Dec 01 Python
教你使用pyinstaller打包Python教程
May 27 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 Warning: Module 'modulename' already loaded in问题解决办法
2015/03/16 PHP
精通Javascript系列之数据类型 字符串
2011/06/08 Javascript
jquery 事件冒泡的介绍以及如何阻止事件冒泡
2012/12/25 Javascript
Json字符串转换为JS对象的高效方法实例
2013/05/01 Javascript
解析javascript 数组以及json元素的添加删除
2013/06/26 Javascript
JavaScript prototype 使用介绍
2013/08/29 Javascript
jquery.post用法之type设置问题
2014/02/24 Javascript
jquery统计用户选中的复选框的个数
2014/06/06 Javascript
用原生js做个简单的滑动效果的回到顶部
2014/10/15 Javascript
jQuery实现选项联动轮播效果【附实例】
2016/04/19 Javascript
AngularJS 指令详细介绍
2016/07/27 Javascript
如何解决vue与传统jquery插件冲突
2017/03/20 Javascript
js实现多行文本框统计剩余字数功能
2017/03/28 Javascript
Javascript实现数组中的元素上下移动
2017/04/28 Javascript
JS实现按钮添加背景音乐示例代码
2017/10/17 Javascript
Vue.js与 ASP.NET Core 服务端渲染功能整合
2017/11/16 Javascript
JS中call和apply函数用法实例分析
2018/06/20 Javascript
在Vant的基础上封装下拉日期控件的代码示例
2018/12/05 Javascript
js字符串处理之绝妙的代码
2019/04/05 Javascript
如何在postman测试用例中实现断言过程解析
2020/07/09 Javascript
python实现简单的TCP代理服务器
2014/10/08 Python
Python文件操作,open读写文件,追加文本内容实例
2016/12/14 Python
python 字符串和整数的转换方法
2018/06/25 Python
解决Python print输出不换行没空格的问题
2018/11/14 Python
python异常处理和日志处理方式
2019/12/24 Python
CSS3教程:background-clip和background-origin
2008/10/17 HTML / CSS
纯CSS3实现圆圈动态发光特效动画的示例代码
2021/03/08 HTML / CSS
美国派对用品及装饰品网上商店:Shindigz
2016/07/30 全球购物
Kivari官网:在线购买波西米亚服装
2018/10/29 全球购物
会计演讲稿范文
2014/05/23 职场文书
内勤岗位职责范本
2015/04/13 职场文书
2015年大学辅导员工作总结
2015/05/12 职场文书
幼儿园开学报名通知
2015/07/16 职场文书
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/04/22 无线电
Python常遇到的错误和异常
2021/11/02 Python
Java基于Dijkstra算法实现校园导游程序
2022/03/17 Java/Android