不知道你为什么这样做,但你可以在测试中从index 中减去n,例如:
In []:
n = 1
for index, value in enumerate(range(50)):
if (index-n) % 10 == 0:
print(index,value)
Out[]:
1 1
11 11
21 21
31 31
41 41
只需为您的第二种情况设置n=2,如果n=0 是基本情况。
或者,从枚举中的-n 开始,它会为您提供正确的值(但不同的索引):
In []:
n = 1
for index, value in enumerate(range(50), -n):
if index % 10 == 0:
print(index,value)
Out[]:
0 1
10 11
20 21
30 31
40 41
但你真的不需要enumerate 和% 索引来获取每10个值,假设你想处理任何可迭代的,只需使用itertools.islice(),例如
In []:
import itertools as it
n = 0
for value in it.islice(range(50), n, None, 10):
print(value)
Out[]:
0
10
20
30
40
然后只需更改n的值,例如:
In []:
n = 1
for value in it.islice(range(50), n, None, 10):
print(value)
Out[]:
1
11
21
31
41