卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16939本站已运行3323

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

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

什么是 enumerate() 函数?

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

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

语法

'
enumerate(iterable, start)

参数

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

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

Python 中 enumerate() 函数的使用

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

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

如何使用?

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

为什么使用 enumerate() 函数?

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

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

'
enumerate(iterable, start)

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

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

示例

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

'
# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object of the input list
enumerate_obj = enumerate(inputList)
print(enumerate_obj)

# converting the enumerate object into a list and printing it
print(list(enumerate_obj))

输出

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

'
<enumerate object at 0x7f4e56b553c0>
[(0, \'hello\'), (1, \'tutorialspoint\'), (2, \'python\'), (3, \'codes\')]

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

示例

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

'
# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object starting from the index 15
enumerate_obj = enumerate(inputList, 15)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

输出

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

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

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

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

示例

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

'
# input list
inputList = ["hello", "tutorialspoint", "python", "codes"]

# getting the enumerate object
enumerate_obj = enumerate(inputList)

# traversing through each item in an enumerate object
for i in enumerate_obj:

    # printing the corresponding iterable item
    print(i)

输出

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

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

在元组上使用 enumerate() 函数

示例

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

'
# input tuple
inputTuple = ("hello", "tutorialspoint", "python", "codes")

# getting the enumerate object of the input tuple
enumerate_obj = enumerate(inputTuple)
print(enumerate_obj)

# converting the enumerate object into the list and printing it
print(list(enumerate_obj))

输出

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

'
<enumerate object at 0x7fec12856410>
[(0, \'hello\'), (1, \'tutorialspoint\'), (2, \'python\'), (3, \'codes\')]

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

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

示例

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

'
# input string
inputString = "python"

# getting the enumerate object of an input string
enumerate_obj = enumerate(inputString)

# converting the enumerate object into the list and printing it
print(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() 函数的开始计数或索引。

卓越飞翔博客
上一篇: Python脚本用于监控网络连接并保存到日志文件中
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏