枚举从 1
Enumeration from 1
是否可以使用枚举函数从 1 开始计算迭代次数?如果没有,如何有效地做到这一点?我正在使用一段特定的代码:
for nr, month in enumerate(range(1, 13)):
print "Month: ", nr
我需要枚举从 1 到 12 的月份。
是的,只需告诉enumerate()
从哪里开始;它需要第二个参数:
for nr, month in enumerate(range(1, 13), 1):
enumerate(sequence, start=0)
[...] The next()
method of the iterator returned by enumerate()
returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence
大胆强调我的。
并不是说您真的需要它,您已经生成了一系列数字,在这种情况下您可以简单地重复使用 month
,因为 nr
和 month
将始终现在平等了。
是否可以使用枚举函数从 1 开始计算迭代次数?如果没有,如何有效地做到这一点?我正在使用一段特定的代码:
for nr, month in enumerate(range(1, 13)):
print "Month: ", nr
我需要枚举从 1 到 12 的月份。
是的,只需告诉enumerate()
从哪里开始;它需要第二个参数:
for nr, month in enumerate(range(1, 13), 1):
enumerate(sequence, start=0)
[...] Thenext()
method of the iterator returned byenumerate()
returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over sequence
大胆强调我的。
并不是说您真的需要它,您已经生成了一系列数字,在这种情况下您可以简单地重复使用 month
,因为 nr
和 month
将始终现在平等了。