"enumerate()"函数在Python中的用途是什么?

"enumerate()"函数在Python中的用途是什么?

在本文中,我们将了解 enumerate() 函数以及 Python 中“enumerate()”函数的用途。

什么是 enumerate() 函数?

Python 的 enumerate() 函数接受数据集合作为参数并返回一个枚举对象。

枚举对象以键值对的形式返回。 key是每个item对应的索引,value是items。

语法

enumerate(iterable, start)

登录后复制登录后复制

参数

iterable – 传入的数据集合可以作为枚举对象返回,称为iterable

立即学习“Python免费学习笔记(深入)”;

start – 顾名思义,枚举对象的起始索引由 start 定义。如果我们忽略这个值,它将认为第一个索引为零,因为零是这里的默认值

Python 中 enumerate() 函数的使用

什么时候使用 enumerate() 函数?

如果需要迭代值的索引,则使用 enumerate() 函数。

如何使用?

枚举函数将每个可迭代值的索引分配给列表。

为什么使用 enumerate() 函数?

使用迭代器时,我们总是需要知道已经完成了多少次迭代,即迭代次数。

但是,我们有一种Pythonic方法来确定必要的迭代次数。语法如下 –

enumerate(iterable, start)

登录后复制登录后复制

如何在Python中使用enumerate()函数?

重要的是要注意返回后将使用哪种数据类型来存储枚举对象。

示例

以下程序在列表上使用 enumerate() 函数来获取带有索引的元组列表 –

# input listinputList = ["hello", "tutorialspoint", "python", "codes"]# getting the enumerate object of the input listenumerate_obj = enumerate(inputList)print(enumerate_obj)# converting the enumerate object into a list and printing itprint(list(enumerate_obj))

登录后复制

输出

执行时,上述程序将生成以下输出 –

[(0, 'hello'), (1, 'tutorialspoint'), (2, 'python'), (3, 'codes')]

登录后复制登录后复制

使用 enumerate() 函数的第二个参数“起始索引”

示例

以下程序显示了如何将 enumerate() 函数应用于包含第二个参数的列表 –

# input listinputList = ["hello", "tutorialspoint", "python", "codes"]# getting the enumerate object starting from the index 15enumerate_obj = enumerate(inputList, 15)# converting the enumerate object into the list and printing itprint(list(enumerate_obj))

登录后复制

输出

执行时,上述程序将生成以下输出 –

[(15, 'hello'), (16, 'tutorialspoint'), (17, 'python'), (18, 'codes')]

登录后复制

在上面的示例中,我们在 enumerate() 函数中添加了 15 作为第二个参数。第二个参数将指示枚举器对象中键(索引)的起始索引,因此我们可以在输出中看到第一个索引为 15,第二个索引为 16,依此类推。

循环遍历枚举对象的 Python 代码

示例

以下程序展示了如何使用 for 循环遍历枚举对象并打印其中的每个元素 –

# input listinputList = ["hello", "tutorialspoint", "python", "codes"]# getting the enumerate objectenumerate_obj = enumerate(inputList)# traversing through each item in an enumerate objectfor i in enumerate_obj:    # printing the corresponding iterable item    print(i)

登录后复制

输出

执行时,上述程序将生成以下输出 –

(0, 'hello')(1, 'tutorialspoint')(2, 'python')(3, 'codes')

登录后复制

在元组上使用 enumerate() 函数

示例

以下程序展示了如何在给定的输入元组上使用 enumerate() 函数 –

# input tupleinputTuple = ("hello", "tutorialspoint", "python", "codes")# getting the enumerate object of the input tupleenumerate_obj = enumerate(inputTuple)print(enumerate_obj)# converting the enumerate object into the list and printing itprint(list(enumerate_obj))

登录后复制

输出

执行时,上述程序将生成以下输出 –

[(0, 'hello'), (1, 'tutorialspoint'), (2, 'python'), (3, 'codes')]

登录后复制登录后复制

在字符串上使用 enumerate() 函数

使用枚举函数迭代字符串数据以确定每个字符的索引。

示例

以下程序展示了如何在给定的输入字符串上使用 enumerate() 函数 –

# input stringinputString = "python"# getting the enumerate object of an input stringenumerate_obj = enumerate(inputString)# converting the enumerate object into the list and printing itprint(list(enumerate_obj))

登录后复制

输出

执行时,上述程序将生成以下输出 –

[(0, 'p'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]

登录后复制

在 enumerate() 函数中使用另一个参数

示例

以下程序展示了如何在 enumerate() 函数中使用另一个参数 –

input_data = ('Hello', 'TutorialsPoint', 'Website')for count, dataValue in enumerate(input_data,start = 50):   print(count, dataValue)

登录后复制

输出

执行时,上述程序将生成以下输出 –

(50, 'Hello')(51, 'TutorialsPoint')(52, 'Website')

登录后复制

Enumerate 是一个返回枚举对象的 Python 内置函数。

该函数返回一个枚举对象。

要将可迭代对象传递给枚举函数,首先尝试从枚举对象检索数据,然后将其转换为 list()。结果,如上面的示例所示,它返回一个元组列表以及迭代索引。

结论

本文详细介绍了 Python enumerate() 函数的应用。我们还学习了如何将它与各种 Python 数据类型和迭代一起使用。此外,我们还学习了如何修改 enumerate() 函数的开始计数或索引。

以上就是”enumerate()”函数在Python中的用途是什么?的详细内容,更多请关注【创想鸟】其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2225634.html

(0)
上一篇 2025年2月26日 11:20:36
下一篇 2025年2月26日 11:20:52

AD推荐 黄金广告位招租... 更多推荐

相关推荐

发表回复

登录后才能评论