我正在尝试使用for循环运行一个python程序,每次从1到我的列表长度,变量i增加1.在java中,我想要的代码可能如下所示:
for (int i = 0; i < array.length; i++) {
//code goes here
i += //the number i want it to go up by
}
Run Code Online (Sandbox Code Playgroud)
这实际上影响了我的计数器的方式,并允许我有效地跳过我的for循环中的数字,我想尝试运行类似的程序,但在python中.有没有办法用python的内置功能做到这一点,或者如果我想让python以这种方式工作,我是否必须使用while循环和计数器来模拟这个?
为此,您需要一个 while 循环:
i = 0
while i < len(myArray):
# do stuff
if special_case: i+= 1
i += 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |