【发布时间】:2018-11-01 14:50:38
【问题描述】:
我正在跟进 Python 基础的在线课程,但遇到了一个关于生成器的测验: 我可以访问该解决方案,但我不想直接看到它。 我想在不使用枚举但使用生成器的情况下进行如下输出。 我有以下预先编写的代码开始,但我不明白为什么他们把 start=0。 有人可以帮我找到一些线索吗?还是想自己做输出,不知道怎么下手。
预期输出:
Lesson 1: Why Python Programming
Lesson 2: Data Types and Operators
Lesson 3: Control Flow
Lesson 4: Functions
Lesson 5: Scripting
代码:
lessons = ["Why Python Programming", "Data Types and Operators", "Control Flow", "Functions", "Scripting"]
def my_enumerate(iterable, start=0):
# Implement your generator function here
for i, lesson in my_enumerate(lessons, 1):
print("Lesson {}: {}".format(i, lesson))
谢谢!
【问题讨论】:
-
start=0表示具有默认值的参数。仅由您指定要从其开始迭代的索引(我猜)。它与要实现的实际生成器无关。