Python enumerate()

In this tutorial, we are going to learn about Python enumerate() function. This is one of the built-in functions in Python.

在本教程中,我们将学习Python enumerate()函数。 这是Python中的内置函数之一。

Python enumerate() (Python enumerate())

Python enumerate takes a sequence, and then make each element of the sequence into a tuple. The first element of the tuple is the index number. And the second element of the tuple is the value of the sequence.

Python枚举取一个序列,然后将序列的每个元素变成一个元组。 元组的第一个元素是索引号。 元组的第二个元素是序列的值。

So, in short, we can say that enumerate adds a counter with the element of a sequence. The basic syntax of python enumerate function is given below.

因此,简而言之,我们可以说枚举添加了一个带有序列元素的计数器。 python枚举函数的基本语法如下。

  1. enumerate(sequence) : This enumerate function make a enumerate object where index starts from 0.

    enumerate(sequence) :此枚举函数创建一个枚举对象,其中索引从0开始。
  2. enumerate(sequence, i): This makes an enumerate object where the index starts from i.

    enumerate(sequence, i):这使一个枚举对象的索引从i开始。

Python枚举列表 (Python Enumerate List)

In this section, we will see an example to create an enumerate object from a list or any other sequence. In the previous section, we learned about enumerate function which converts a sequence to enumerate object. Let’s see the following example.

在本节中,我们将看到一个从列表或任何其他序列创建枚举对象的示例。 在上一节中,我们了解了枚举函数,该函数将序列转换为枚举对象。 让我们看下面的例子。

# initialize a list of list
data = ['Love', 'Hate', 'Death', 123, ['Alice', 'Bob', 'Trudy']]

# print the type of variable 'data'
print('The type of data is :', type(data))  # output is 'list'

data = enumerate(data)
# again, print the type of variable 'data'
print('The type of data is now :', type(data))  # output is 'enumerate'

The output of the following code will be

以下代码的输出将是

访问Python枚举对象 (Accessing Python Enumerate Object)

We can access the enumerate object. We can use for loop to access the enumerate object. Or, we can convert the enumerate object to a list object.

我们可以访问枚举对象。 我们可以使用for循环来访问枚举对象。 或者,我们可以将枚举对象转换为列表对象。

Then we can traverse the list like we did in our python list tutorial. Let’s have a look to the following example to understand this.

然后,我们可以像在python列表教程中那样遍历列表 。 让我们看下面的例子来理解这一点。

# initialize a list of list
data = ['Love', 'Hate', 'Death', 123, ['Alice', 'Bob', 'Trudy']]
# make an enumerate object
enumObject = enumerate(data)

# access the enumerate object using loop
for element in enumObject:
    print(element)

print('\nStart index is changed to 100:')
# change the start index of the list to 100
enumObject = enumerate(data, 100)

# access the enumerate object using loop
for element in enumObject:
    print(element)

Output:

输出:

(0, 'Love')
(1, 'Hate')
(2, 'Death')
(3, 123)
(4, ['Alice', 'Bob', 'Trudy'])

Start index is changed to 100:
(100, 'Love')
(101, 'Hate')
(102, 'Death')
(103, 123)
(104, ['Alice', 'Bob', 'Trudy'])

So, that’s the basics of Python enumerate function. Usually you may not need it always but it’s not bad to know about new things. For any query regarding this topic, please use the comment box. Happy coding. 🙂

因此,这就是Python枚举函数的基础。 通常,您可能不一定总是需要它,但是了解新事物也不错。 对于有关此主题的任何查询,请使用注释框。 快乐的编码。 🙂

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/15082/python-enumerate

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值