Python中enumerate函数的解释及可视化

enumerate() 的作用 在许多情况下,我们需要在迭代数据对性(即我们可以循环的任何对象)时获取元素的索引。实现预期结果的一种方法是:
animals = ['dog', 'cat', 'mouse']for i in range(len(animals)): print(i, animals[i]) 输出:
0 dog1 cat2 mouse 大多数c ++ / java背景的开发人员都可能会选择上述实现,通过索引迭代数据对象的长度是他们熟悉的概念。但是,这种方法效率很低。
我们可以使用enumerate()来实现:
for i, j in enumerate(example): print(i, j) enumerate()提供了强大的功能,例如,当您需要获取索引列表时,它会派上用场:
(0, seq[0]), (1, seq[1]), (2, seq[2]), ... 案例研究1:枚举字符串  
字符串只是一个列表
为了更好地理解字符串枚举,我们可以将给定的字符串想象为单个字符(项)的集合。因此,枚举字符串将为我们提供:
1.字符的索引。
2.字符的值。
word = speedfor index, char in enumerate(word): print(fthe index is '{index}' and the character value is '{char}') 输出:
the index is '0' and the character value is 's'the index is '1' and the character value is 'p'the index is '2' and the character value is 'e'the index is '3' and the character value is 'e'the index is '4' and the character value is 'd' 案例研究2:列举列表  
那么,我们应该如何列举一个列表呢?为了做到这一点,我们可以利用for循环并遍历每个项目的索引和值:
sports = ['soccer', 'basketball', 't` ennis']for index, value in enumerate(sports): print(fthe item's index is {index} and its value is '{value}') 输出:
the item's index is 0 and its value is 'soccer'the item's index is 1 and its value is 'basketball'the item's index is 2 and its value is 'tennis' 案例研究3:自定义起始索引  
我们可以看到枚举从索引0开始,但是们经常需要更改起始位置,以实现更多的可定制性。值得庆幸的是,enumerate()还带有一个可选参数[start]
enumerate(iterable, start=0) 可以用来指示索引的起始位置,方法如下:
students = ['john', 'jane', 'j-bot 137']for index, item in enumerate(students, start=1): print(fthe index is {index} and the list element is '{item}') 输出
the index is 1 and the list element is 'john'the index is 2 and the list element is 'jane'the index is 3 and the list element is 'j-bot 137' 现在,修改上述代码:1.起始索引可以为负;2.省略start=则默认从0索引位置开始。
teachers = ['mary', 'mark', 'merlin']for index, item in enumerate(teachers, -5): print(fthe index is {index} and the list element is '{item}') 输出将是:
the index is -5 and the list element is 'mary'the index is -4 and the list element is 'mark'the index is -3 and the list element is 'merlin' 案例研究4:枚举元组 使用枚举元组遵循与枚举列表相同的逻辑:
colors = ('red', 'green', 'blue')for index, value in enumerate(colors): print(fthe item's index is {index} and its value is '{value}') 输出:
the item's index is 0 and its value is 'red'the item's index is 1 and its value is 'green'the item's index is 2 and its value is 'blue' 案例研究5:枚举列表中的元组  
让我们提高一个档次,将多个元组合并到一个列表中……我们要枚举此元组列表。一种做法的代码如下:
letters = [('a', 'a'), ('b', 'b'), ('c', 'c')]for index, value in enumerate(letters): lowercase = value[0] uppercase = value[1] print(findex '{index}' refers to the letters '{lowercase}' and '{uppercase}') 但是,元组拆包被证明是一种更有效的方法。比如:
letters = [('a', 'a'), ('b', 'b'), ('c', 'c')]for i, (lowercase, uppercase) in enumerate(letters): print(findex '{i}' refers to the letters '{lowercase}' and '{uppercase}') 输出:
index '0' refers to the letters 'a' and 'a'index '1' refers to the letters 'b' and 'b'index '2' refers to the letters 'c' and 'c' 案例研究6:枚举字典  
枚举字典似乎类似于枚举字符串或列表,但事实并非如此,主要区别在于它们的顺序结构,即特定数据结构中元素的排序方式。
字典有些随意,因为它们的项的顺序是不可预测的。如果我们创建字典并打印它,我们将得到一种结果:
translation = {'one': 'uno', 'two': 'dos', 'three': 'tres'}print(translation)# output on our computer: {'one': 'uno', 'two': 'dos', 'three': 'tres'} 但是,如果打印此词典,则顺序可能会有所不同!
由于索引无法访问字典项,因此我们必须利用for循环来迭代字典的键和值。该key — value对称为item,因此我们可以使用.items()方法:
animals = {'cat': 3, 'dog': 6, 'bird': 9}for key, value in animals.items(): print(key, value) 输出将是:
cat 3dog 6bird 9  
本文由博客一文多发平台 openwrite 发布!

大型商场POS作业系统以及贵重商品的电子防窃设计
中国移动在北京地区开通首个双频5G基站5G网络下行速率达5Gbps以上
5G LAN标准进展,5G LAN应用场景需求分析
博通并购高通影响太大,被阻未能并购成功
人工智能核心涉知识、数据、算法和算力
Python中enumerate函数的解释及可视化
气动球阀结构及工作原理
未来五年中国计划投入9.5万亿来研发自主芯片
【算法+终端】EC-A1684JD4生态算法一体机
中芯国际14纳米FinFET研发完成,良率达到95%
美国空军融合多种光电技术增强监视雷达能力
24C01读写的c51源程序代码分享
如何避免PCB设计限制D类放大器性能?
DC电源模块超宽电压输入和输出的问题
无人沙滩清理车横空出世,沙滩清理的小能手
汽车故障诊断UDS网络层原理讲解
结合两种流行的机器学习方法,创造了一种新的人工智能程序
未来3年,上海预计新增1万台机器人
航电总线接口革命
Pure Storage或被戴尔、联想收购 其股价应声上涨,涨幅5.2%