Python “enumerate()”函数有哪些用途
在本文中,我们将学习enumerate()函数以及在Python中“enumerate()”函数的用途。
enumerate()函数是什么
Python的enumerate()函数接受一个数据集合作为参数,并返回一个enumerate对象。
枚举对象以键-值对的格式返回。键是每个项的对应索引,值是项。
语法
enumerate(iterable, start)
参数
- iterable - 传入的数据集合,可以被返回为enumerate对象的数据集合被称为可迭代对象
-
start - 如其名所示,枚举对象的起始索引由start定义。如果忽略该值,则会将第一个索引视为0,因为0是默认值
在Python中使用enumerate()函数的用途
什么时候使用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,后面的索引依次递增。
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函数迭代字符串数据,以确定每个字符的索引。
示例
以下程序展示了如何在给定的输入字符串上使用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内置函数,返回一个枚举对象。
-
这个函数返回一个枚举对象。
-
要将可迭代对象传递给enumerate函数,首先尝试从enumerate对象中检索数据,然后将其转换为list()。因此,正如上面的示例所示,它返回了一个元组列表以及迭代索引。
结论
在本文中,我们详细介绍了Python的enumerate()函数的应用。我们还学习了如何与各种Python数据类型和可迭代对象一起使用它。此外,我们还学习了如何修改enumerate()函数的初始计数或索引。