Python中的for循环是不允许改变迭代进程的,看下面的例子:
wordlist = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
i = 0
for i in range(len(wordlist)):
print(wordlist[i])
i += 3
然后输出结果是:
1 2 3 4 5 6 7 8 9 10
这是因为在python的for循环中,循环域内改变i的值,只能改变当前这一次迭代中的i值,也就是说for循环只能作完整的顺序迭代,是不能跳跃处理的,如果要跳跃处理,可以使用while循环
wordlist = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
i = 0
while i < len(wordlist):
print(wordlist[i])
i += 3
这样的话修改i的值就会在域内和循环条件中同时起作用,运行结果:
1 4 7 10