简体   繁体   English

循环遍历数组但跳过一些索引

[英]Looping over an array but skipping some of the index

I have the following loop which works when creating an output file.我有以下循环在创建输出文件时有效。 There are n-number of rows and 24 columns and it's going each row and column to create plots.有 n 行和 24 列,它会在每一行和每一列中创建图。 variable below is from index [3:24].下面的variable来自索引 [3:24]。 Now the variable is created somewhere in before this loop in the code that I don't want to touch.现在该variable是在我不想触及的代码中此循环之前的某处创建的。 I want to be able to overwrite the variable and not go over some of the indexes from [3:24] such as index 22, 20, 3. How can I implement that below?我希望能够覆盖variable ,而不是遍历 [3:24] 中的某些索引,例如索引 22、20、3。我该如何在下面实现呢? I tried creating another if statement so that when it reaches index 20 it would break.我尝试创建另一个 if 语句,以便当它到达索引 20 时它会中断。 Anyways my solution doesn't work so I was hoping to get an answer here.无论如何,我的解决方案不起作用,所以我希望在这里得到答案。 Thanks!谢谢!

for row in range(len(self.CSVResult)):
    if (abs(self.CSVResult[row][variable-1]) < 1.2E20):
       skipsw = 1
       break

For example, variable is looping from index 3 to 24, once it reaches index 22 it will skip and continue to 21. Then once it reaches index 20 it will skip and reach 19. Until it reach index 3 again and then skip that value too.例如, variable从索引 3 循环到 24,一旦到达索引 22,它将跳过并继续到 21。然后一旦到达索引 20,它将跳过并到达 19。直到它再次到达索引 3,然后也跳过该值.

Does something like this help?这样的事情有帮助吗?

skip_values = [22, 21, 3]
for row in range(len(self.CSVResult)):
    if variable in skip_values:
        continue
    elif (abs(self.CSVResult[row][variable-1]) < 1.2E20):
        skipsw = 1
        break

If I understood correctly, you want to define some 'skip values', where if variable is equal one of those, then it does not do the rest of the code.如果我理解正确,您想定义一些“跳过值”,如果变量等于其中之一,那么它不会执行其余代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM