python之enumerate()函数使用总结

        enumerate() 函数是一个内置函数,用于在迭代过程中同时获取元素的索引和值。它返回一个枚举对象,包含了索引和对应的元素。

1. enumerate() 函数的语法

如下:

enumerate(iterable, start=0)

        参数说明:

                iterable:必需,一个可迭代对象,如列表、元组、字符串等。

                start:可选,指定索引的起始值,默认为 0。

2. enumerate() 函数的用法

2.1 基本用法

fruits = ['apple', 'banana', 'orange']
for index, fruit in enumerate(fruits):
    print(index, fruit)

输出结果:

0 apple
1 banana
2 orange

2.2 指定起始索引

fruits = ['apple', 'banana', 'orange']

for index, fruit in enumerate(fruits, start=1):
    print(index, fruit)

输出结果:

1 apple
2 banana
3 orange

2.3 结合解包(unpacking)

for index, _ in enumerate(fruits):
    print(index)

输出结果:

0
1
2

在上述情况下,可以使用下划线 _ 来表示不需要的值,以减少内存消耗。

2.4 枚举对象转换为列表或元组

enum_list = list(enumerate(fruits))
enum_tuple = tuple(enumerate(fruits))

print(enum_list)
print(enum_tuple)

输出结果:

[(0, 'apple'), (1, 'banana'), (2, 'orange')]
((0, 'apple'), (1, 'banana'), (2, 'orange'))

可以使用 list() 或 tuple() 函数将枚举对象转换为列表或元组。

上述是 enumerate() 函数的一些常见用法。

当使用enumerate()函数时,还可以结合其他常用的Python函数和技巧来实现更多功能。

2.5 获取特定条件的元素索引

fruits = ['apple', 'banana', 'orange']

# 查找元素为'banana'的索引
index = next((index for index, fruit in enumerate(fruits) if fruit == 'banana'), None)
print(index)

输出结果: 1

在上述示例中,使用了生成器表达式和next()函数来查找列表中特定元素('banana')的索引。

2.6 反向遍历列表并获取索引

for index, fruit in enumerate(reversed(fruits)):
    print(index, fruit)

输出:

0 orange
1 banana
2 apple

通过使用reversed()函数,可以反向遍历列表并获取相应的索引和值。

2.7 枚举多个可迭代对象

fruits = ['apple', 'banana', 'orange']
prices = [1.0, 0.5, 0.8]

for index, (fruit, price) in enumerate(zip(fruits, prices)):
    print(index, fruit, price)

输出结果:

0 apple 1.0
1 banana 0.5
2 orange 0.8

在上述示例中,使用了zip()函数将多个可迭代对象(fruits和prices)进行组合,并使用enumerate()获取索引和对应的值。

2.8 枚举字典的键值对

fruits = {'apple': 1.0, 'banana': 0.5, 'orange': 0.8}

for index, (fruit, price) in enumerate(fruits.items()):
    print(index, fruit, price)
输出结果:
0 apple 1.0
1 banana 0.5
2 orange 0.8

        通过使用items()方法,可以将字典的键值对转换为可迭代对象,并使用enumerate()获取索引和对应的键值对。

       上述是一些扩展的enumerate()函数用法示例,可以根据具体需求进行灵活运用。enumerate()函数在处理需要索引的迭代过程中非常有用,可以简化代码并提高可读性。

相关推荐

  1. pythonenumerate()函数使用总结

    2024-05-14 18:34:13       13 阅读
  2. Python基础总结enumerate介绍使用

    2024-05-14 18:34:13       6 阅读
  3. python内置函数enumerate()、eval()详解

    2024-05-14 18:34:13       20 阅读
  4. Python中的enumerate函数详解

    2024-05-14 18:34:13       11 阅读
  5. [AIGC] 深入浅出 Python中的`enumerate`函数

    2024-05-14 18:34:13       9 阅读
  6. pythonSimpleNamespace()使用总结

    2024-05-14 18:34:13       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-14 18:34:13       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-14 18:34:13       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-14 18:34:13       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-14 18:34:13       18 阅读

热门阅读

  1. C++ LCR 090. 打家劫舍 II

    2024-05-14 18:34:13       14 阅读
  2. js 文档片段 DocumentFragment

    2024-05-14 18:34:13       16 阅读
  3. 深度学习关键概念理解

    2024-05-14 18:34:13       10 阅读
  4. rust类型和变量(二)

    2024-05-14 18:34:13       9 阅读
  5. 一个长期后台运行的服务

    2024-05-14 18:34:13       10 阅读
  6. NLP(15)-序列标注任务

    2024-05-14 18:34:13       9 阅读
  7. 单链表与双链表

    2024-05-14 18:34:13       8 阅读
  8. 蓝桥杯单片机组——国赛1 各模块的基础模板

    2024-05-14 18:34:13       13 阅读
  9. 微信小程序-禁止页面下拉回弹

    2024-05-14 18:34:13       12 阅读
  10. Frida逆向与利用自动化

    2024-05-14 18:34:13       11 阅读