enumerate() 的作用:
**在许多情况下,我们需要在迭代数据对性(即我们可以循环的任何对象)时获取元素的索引。实现预期结果的一种方法是:

  1. animals = ['dog', 'cat', 'mouse']
  2. for i in range(len(animals)):
  3. print(i, animals[i])
  4. Out:
  5. 0 dog
  6. 1 cat
  7. 2 mouse

这种方式我们可以用enumerate实现:

  1. for i, j in enumerate(example):
  2. print(i, j)

enumerate()提供了强大的功能,例如,当你需要获取索引列表时,它会派上用场:

(0, seq[0]), (1, seq[1]), (2, seq[2]), ...

1.案例研究1:枚举字符串

image.png
字符串只是一个列表
**
为了更好地理解字符串枚举,我们可以将给定的字符串想象为单个字符(项)的集合。因此,枚举字符串将为我们提供:

1.字符的索引。2.字符的值。

  1. word = "Speed"
  2. for index, char in enumerate(word):
  3. print(f"The index is '{index}' and the character value is '{char}'")
  4. Out:
  5. The index is '0' and the character value is 'S'
  6. The index is '1' and the character value is 'p'
  7. The index is '2' and the character value is 'e'
  8. The index is '3' and the character value is 'e'
  9. The index is '4' and the character value is 'd'

2.案例研究2:列举列表

image.png


那么,我们应该如何列举一个列表呢?为了做到这一点,我们可以利用for循环并遍历每个项目的索引和值:

  1. sports = ['soccer', 'basketball', 't` ennis']
  2. for index, value in enumerate(sports):
  3. print(f"The item's index is {index} and its value is '{value}'")
  4. Out:
  5. The item's index is 0 and its value is 'soccer'
  6. The item's index is 1 and its value is 'basketball'
  7. The item's index is 2 and its value is 'tennis'

3.案例研究3:自定义起始索引

image.png

我们可以看到枚举从索引0开始,但是们经常需要更改起始位置,以实现更多的可定制性。值得庆幸的是,enumerate()还带有一个可选参数[start]。

enumerate(iterable, start=0)

可以用来指示索引的起始位置,方法如下:

  1. students = ['John', 'Jane', 'J-Bot 137']
  2. for index, item in enumerate(students, start=1):
  3. print(f"The index is {index} and the list element is '{item}'")
  4. Out:
  5. The index is 1 and the list element is 'John'
  6. The index is 2 and the list element is 'Jane'
  7. The index is 3 and the list element is 'J-Bot 137'

现在,修改上述代码:1.起始索引可以为负;2.省略start=则默认从0索引位置开始。

  1. teachers = ['Mary', 'Mark', 'Merlin']
  2. for index, item in enumerate(teachers, -5):
  3. print(f"The index is {index} and the list element is '{item}'")
  4. Out:
  5. The index is -5 and the list element is 'Mary'
  6. The index is -4 and the list element is 'Mark'
  7. The index is -3 and the list element is 'Merlin'

4.案例研究4:枚举元组

使用枚举元组遵循与枚举列表相同的逻辑:

  1. colors = ('red', 'green', 'blue')
  2. for index, value in enumerate(colors):
  3. print(f"The item's index is {index} and its value is '{value}'")
  4. Out:
  5. The item's index is 0 and its value is 'red'
  6. The item's index is 1 and its value is 'green'
  7. The item's index is 2 and its value is 'blue'

5.案例研究5:枚举列表中的元组

image.png
让我们提高一个档次,将多个元组合并到一个列表中……我们要枚举此元组列表。一种做法的代码如下:

  1. letters = [('a', 'A'), ('b', 'B'), ('c', 'C')]
  2. for index, value in enumerate(letters):
  3. lowercase = value[0]
  4. uppercase = value[1]
  5. print(f"Index '{index}' refers to the letters '{lowercase}' and '{uppercase}'")

但是,元组拆包被证明是一种更有效的方法。比如:

  1. letters = [('a', 'A'), ('b', 'B'), ('c', 'C')]
  2. for i, (lowercase, uppercase) in enumerate(letters):
  3. print(f"Index '{i}' refers to the letters '{lowercase}' and '{uppercase}'")
  4. Out:
  5. Index '0' refers to the letters 'a' and 'A'
  6. Index '1' refers to the letters 'b' and 'B'
  7. Index '2' refers to the letters 'c' and 'C'

6.案例研究6:枚举字典

image.png
枚举字典似乎类似于枚举字符串或列表,但事实并非如此,主要区别在于它们的顺序结构,即特定数据结构中元素的排序方式。

字典有些随意,因为它们的项的顺序是不可预测的。如果我们创建字典并打印它,我们将得到一种结果:

  1. animals = {'cat': 3, 'dog': 6, 'bird': 9}
  2. for key, value in animals.items():
  3. print(key, value)
  4. Out:
  5. cat 3
  6. dog 6
  7. bird 9