python的枚举函数_讲解Python枚举enumerate()函数及用enumerate()编写更多代码

本文为你讲解Python枚举:Python enumerate()函数及使用enumerate()编写更多Pythonic代码,以下内容适用于Linux平台中。enumerate()是Python中的内置函数,可让你在遍历可迭代对象时拥有一个自动计数器。

Python enumerate()函数

enumerate()函数采用以下形式:

enumerate(iterable, start=0)

该函数接受两个参数:

1]、iterable-支持迭代的对象。

2]、start-计数器开始的编号,此参数是可选的,默认情况下,计数器从0开始。

enumerate()返回一个枚举对象,你可以在该对象上调用__next__()(或Python 2中的next())方法来获取一个包含计数和可迭代对象当前值的元组。

这是一个如何使用list()创建元组列表以及如何遍历可迭代对象的示例:

directions = ["north", "east", "south", "west"]

list(enumerate(directions))

for index, value in enumerate(directions):

print("{}: {}".format(index, value))

返回:

[(0, 'north'), (1, 'east'), (2, 'south'), (3, 'west')]

0: north

1: east

2: south

3: west

如果从零开始的索引不适合你,请为枚举选择另一个起始索引:

directions = ["north", "east", "south", "west"]

list(enumerate(directions, 1))

返回:

[(1, 'north'), (2, 'east'), (3, 'south'), (4, 'west')]

enumerate()函数可用于任何可迭代对象,可迭代的是可以迭代的容器,简单来说,它意味着可以使用for循环遍历的对象,Python中的大多数内置对象(例如字符串、列表和元组)都是可迭代的。

使用enumerate()编写更多Pythonic代码

Python的for循环与许多编程语言都可以使用的传统C风格的for循环完全不同,Python中的for循环等效于其他语言的foreach循环。

新Python开发人员在处理可迭代对象时用于获取相应索引的常用技术是使用range(len(...))模式或设置并增加计数器:

planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]

for i in range(len(planets)):

print("Planet {}: {}".format(i, planets[i]))

planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]

i = 0

for planet in planets:

print("Planet {}: {}".format(i, planet))

i += 1

可以使用enumerate()以更惯用的方式重写上面的循环:

planets = ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]

for index, value in enumerate(planets):

print("Planet {}: {}".format(index, value))

所有方法将产生相同的输出:

Planet 0: Mercury

Planet 1: Venus

Planet 2: Earth

Planet 3: Mars

Planet 4: Jupiter

Planet 5: Saturn

Planet 6: Uranus

Planet 7: Neptune

结论

在本文中,我们向你展示了如何使用Python的enumerate()函数的基本方法。

相关主题

Python枚举函数enumerate()是一个内置函数,它将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,并同时列出数据和数据下标,通常用在for循环中。该函数返回一个enumerate对象,是一个可迭代对象,通过遍历可以获取具体的元素值。它的语法为:enumerate(iterable, start),其中iterable是可遍历的对象,start是索引的起始值,默认为0。使用enumerate函数可以在for循环中同时获取索引和值。 这个函数在字典上也可以使用,表示枚举或列举的意思,用于获取字典中的键和值。对于一个可迭代的对象,可以使用enumerate函数将其组合为一个索引序列,从而在for循环中获得计数的效果。 一个例子是可以利用enumerate()函数和for循环来遍历文件的每一行,并同时获得行号和行内容。例如,可以使用以下代码来读取一个文件,并在遍历每一行的同时记录行号: count = 0 for index, line in enumerate(open(filepath,'r')): count += 1 这样就可以在count变量中获得文件的行数,index变量中获得行号,line变量中获得每一行的内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python 使用enumerate()函数详解](https://blog.csdn.net/jh035/article/details/128077895)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python enumerate用法总结](https://blog.csdn.net/churximi/article/details/51648388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值