Python enumerate 函数 - Python零基础入门教程 悠悠 2022-10-19 15:59 272阅读 0赞 目录 * 一.Python enumerate 函数介绍 * 二.Python enumerate 函数使用 * 1.Python enumerate 函数案例 1 * 2.Python enumerate 函数案例 2 * 三.猜你喜欢 > 零基础 Python 学习路线推荐 : [Python 学习目录][Python] >> [Python 基础入门][Python 1] ## 一.Python enumerate 函数介绍 ## **[enumerate][] 函数一般用在 [for 循环][for]中,用于遍历一个序列([列表 list][list] 或者 [元组 tuple][tuple]),该函数会同时返回元素下标和对应的元素**,语法如下: # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python enumerate 函数.py @Time:2021/04/15 07:37 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ ''' 参数介绍: iterable — 可迭代对象(列表、元祖、字典、迭代器、字符串等); start — 索引序列的起始值,默认重0开始; 返回值:同时返回元素下标和对应的元素; ''' enumerate(iterable,start=0) ## 二.Python enumerate 函数使用 ## ### 1.Python enumerate 函数案例 1 ### # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python enumerate 函数.py @Time:2021/04/15 07:37 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ a = ['one','three','five','seven','nine'] for index,i in enumerate(a): print(index,i) ''' 输出结果: 0 one 1 three 2 five 3 seven 4 nine ''' ### 2.Python enumerate 函数案例 2 ### # !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python enumerate 函数.py @Time:2021/04/15 07:37 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ b = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] for index,i in enumerate(b,1): print("星期%d:%s"%(index,i)) ''' 输出结果: 星期1:Monday 星期2:Tuesday 星期3:Wednesday 星期4:Thursday 星期5:Friday 星期6:Saturday 星期7:Sunday ''' ## 三.**猜你喜欢** ## 1. [Python 字符串/列表/元组/字典之间的相互转换][Python 2] 2. [Python 局部变量和全局变量][Python 3] 3. [Python type 函数和 isinstance 函数区别][Python type _ isinstance] 4. [Python is 和 == 区别][Python is _ _] 5. [Python 可变数据类型和不可变数据类型][Python 4] 6. [Python 浅拷贝和深拷贝][Python 5] 7. [Python 递归函数][Python 6] 8. [Python sys 模块][Python sys] 9. [Python 列表 list][list] 10. [Python 元组 tuple][tuple] 11. [Python 字典 dict][Python _ dict] 12. [Python 条件推导式][Python 7] 13. [Python 列表推导式][Python 8] 14. [Python 字典推导式][Python 9] 15. [Python 函数声明和调用][Python 10] 16. [Python 不定长参数 \*argc/\*\*kargcs][Python _ _argc_kargcs] 未经允许不得转载:[猿说编程][Link 1] » [Python enumerate 函数][enumerate] [Python]: https://www.codersrc.com/python [Python 1]: https://www.codersrc.com/pythonsimple [enumerate]: https://www.codersrc.com/archives/5860.html [for]: https://www.codersrc.com/archives/4953.html [list]: https://www.codersrc.com/archives/5061.html [tuple]: https://www.codersrc.com/archives/5088.html [Python 2]: https://www.codersrc.com/archives/5137.html [Python 3]: https://www.codersrc.com/archives/5156.html [Python type _ isinstance]: https://www.codersrc.com/archives/5214.html [Python is _ _]: https://www.codersrc.com/archives/5288.html [Python 4]: https://www.codersrc.com/archives/5271.html [Python 5]: https://www.codersrc.com/archives/5492.html [Python 6]: https://www.codersrc.com/archives/5514.html [Python sys]: https://www.codersrc.com/archives/5535.html [Python _ dict]: https://www.codersrc.com/archives/5114.html [Python 7]: https://www.codersrc.com/archives/5346.html [Python 8]: https://www.codersrc.com/archives/5359.html [Python 9]: https://www.codersrc.com/archives/5377.html [Python 10]: https://www.codersrc.com/archives/5405.html [Python _ _argc_kargcs]: https://www.codersrc.com/archives/5443.html [Link 1]: https://www.codersrc.com
还没有评论,来说两句吧...