【Python】内置函数 enumerate
Python 函数 内置 enumerate
2023-09-14 08:57:29 时间
介绍
如何使用该函数
运行结果
注意 Dict 和Str 使用 enmerate 函数的起始值分别从2 1 开始的。
# enumerate 将可迭代序列中的元素所在的下标和具体元素数据组合在一起,变成元组 for j in enumerate(my_list):
在解析mysqlbinlog dump出来的binlog的时候学习了一个函数 --enumerate。官方的定义如下:
def enumerate(collection,N=0): Generates an indexed series: (0,coll[0]), (1,coll[1]) ... i = N it = iter(collection) while 1: yield (i, it.next()) i += 1
N 是索引起始值 比如 enumerate(list,2) 索引是从2 开始。
通常我们需要遍历序列如 字符串,字典,列表,也要遍历其索引时,我们会使用for 循环来解决
for i in range (0,len(list)): print i ,list[i]使用内置enumerrate函数会有更加直接,优美的做法
for idx,name in enumerate(list)): print idx,name
如何使用该函数
#coding=utf-8 List = [a, b, c] print (list(enumerate(List))) Tuple = (youzan, SAAS, work,Mac) print(list(enumerate(Tuple))) Dict = {"city":"HANGZHOU", company:"youzan", dba:yangyi} print(list(enumerate(Dict, 2))) Str = YOUZAN! print(list(enumerate(Str, 1)))
运行结果
注意 Dict 和Str 使用 enmerate 函数的起始值分别从2 1 开始的。
# enumerate 将可迭代序列中的元素所在的下标和具体元素数据组合在一起,变成元组 for j in enumerate(my_list):
相关文章
- 关于Excel,你一定用的到的36个Python函数
- 第二十六节,Python内置函数
- Python操作Mysql实例代码教程在线版(查询手册)_python
- Python不使用int()函数把字符串转换为数字
- 小白学 Python(16):基础数据类型(函数)(上)
- 【Python五篇慢慢弹(3)】函数修行知python
- Python编程语言学习:python的列表的特殊应用之一行命令实现if判断中的两类判断
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python语言学习之字母E开头函数使用集锦:endswith使用用法之详细攻略
- Python 没有函数重载?如何用装饰器实现函数重载?
- 实用且高效,必须要掌握的10个Python内置函数
- 这 10 个超实用的 Python 函数,你不妨试试!
- python hasattr()函数,getattr()函数, setattr()函数
- python 内置函数bytearray
- Python标准库:内置函数exec(object[, globals[, locals]])
- Python标准库:内置函数open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=T
- Python标准库:内置函数globals()
- Python标准库:内置函数hash(object)
- Python标准库:内置函数help([object])
- Python标准库:内置函数int(x, base=10)
- Python标准库:内置函数memoryview(obj)
- Python标准库:内置函数sorted(iterable[, key][, reverse])
- Python标准库:内置函数str(object='') str(object=b'', encoding='utf-8', errors='strict')
- Python标准库:内置函数type(object)
- linux bash shell 中getopts 命令 和 python 中 getopt 函数的比较总结
- Python开发入门之了解Python高阶函数