对于循环计数器不递增

时间:2019-02-15 14:07:46

标签: python

我有一个双for循环来遍历矩阵并比较数字。 RowCounter在我的循环中没有增加,我也看不出为什么。我还重新创建了每一行前面的空格,以使这部分没有被“破坏”。因此,在Notepad ++中,我在第一个for循环的前面获得了“加号”,以便可以关闭整个循环及其内部内容。基本上,我的2个for循环是这样做的:

RowCounter=0 ColCounter=0
RowCounter=0 ColCounter=1
RowCounter=0 ColCounter=2
RowCounter=0 ColCounter=3
RowCounter=0 ColCounter=0
RowCounter=0 ColCounter=1
RowCounter=0 ColCounter=2
RowCounter=0 ColCounter=3

我真的不明白为什么RowCounter不升为1。

Rows=2
Cols=5
for RowCounter in range(0, Rows-1, 1):
    for ColCounter in range(0, Cols-1, 1):
        CurrentValue=float(Numbers[RowCounter][ColCounter])                  
        if CurrentValue==NoDataValue:
            DataMatrix[RowCounter][ColCounter]=float(0.0)
        else:
            DataMatrix[RowCounter][ColCounter]=float(CurrentValue)

1 个答案:

答案 0 :(得分:0)

  

class range(start, stop[, step])

     

[...]

     

对于正步,范围r的内容由公式r [i] = start + step * i确定,其中i> = 0且r [i] <停止。

Python3 documentation

从使用角度来看,for x in range(start, stop, step)可以解释如下:

# This is an explanation, use for ... in range
x = start
while x < stop:
     # ...
     x += step

因此,您的停留边界是错误的。