英文:
Can't count iterations on double for loop in python
问题
以下是翻译好的部分:
我在实现一个用于计数双重循环迭代的计数器时遇到了问题。
我的代码如下:
def encode(mat, l, c, mode, m):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
print(sampleCount)
我的程序使用分别为720和360的“l”值以及分别为1280和640的“c”值调用此函数。我预期的是sampleCount的值分别为921600和230400。然而,它却打印出了1279或639。
此外,当我像这样测试打印i和j时:
for i in range(l):
print(i)
for j in range(c):
print(j)
我得到的结果是程序首先打印出所有i值,从0到l-1,然后才打印出j值,从0到c-1。
有人可以告诉我可能出了什么问题吗?提前谢谢!
编辑:粘贴代码时没有缩进
编辑2:尝试在sampleCount += 1之后注释掉所有内容。在这种情况下,我得到了预期的结果。如果取消注释下面的两行代码,它会继续正常工作。然而,当我尝试取消注释超过3行的代码时,它会再次出现问题。简而言之,当代码像这样时它可以工作:
def encode2(mat, l, c, mode, m):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
a = 0
b = 0
# c = 0
# x = 0
# if (i == 0 & j == 0):
# a = 0
# b = 0
# c = 0
... ...
并且如果代码像这样时它会再次出现问题:
def encode2(mat, l, c, mode, m):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
a = 0
b = 0
c = 0
# x = 0
# if (i == 0 & j == 0):
# a = 0
# b = 0
# c = 0
... ...
希望这有助于解决你的问题。
英文:
I am having trouble implementing a counter to count over iterations on a double for loop.
My code is the following:
def encode(mat,l,c,mode,m):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
print(sampleCount)
My program calls this function with values of "l" of 720 and 360 and values of "c" of 1280 and 640 respectively. What I was expecting was sampleCount values of 921600 and 230400. However, it prints either 1279 or 639.
Also, when i tested printing i and j like this:
for i in range(l):
print(i)
for j in range(c):
print(j)
What I get is the program printing all the i values, from 0 to l-1 and only then printing the j values from 0 to c-1.
Can anyone tell me what I may be doing wrong? Thanks in advance!
Edit: Pasted code without identation
Edit 2: Tried commenting everything after sampleCount += 1. In that case, i obtain the expected results. And it continues to work well if i uncomment the following two lines of code. However, when i tried uncommenting more than 3 lines of code, it goes back to misbehaving. In short, it works when the code is like this:
def encode2(mat,l,c,mode,m):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
a = 0
b = 0
# c = 0
# x = 0
# if (i == 0 & j == 0):
# a = 0
# b = 0
# c = 0
... ...
And misbehaves again if the code is like this:
def encode2(mat,l,c,mode,m):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
a = 0
b = 0
c = 0
# x = 0
# if (i == 0 & j == 0):
# a = 0
# b = 0
# c = 0
... ...
答案1
得分: 1
以下是翻译好的代码部分:
我运行相同的代码时得到了以下结果:
def encode(l,c):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
print(sampleCount)
encode(360,640)
结果:230400
def encode(l,c):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
print(sampleCount)
encode(720,1280)
结果:921600
您的期望也是这样吗?
英文:
I got the following result when I ran the same code:
def encode(l,c):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
print(sampleCount)
encode(360,640)
Result: 230400
def encode(l,c):
sampleCount = 0
for i in range(l):
for j in range(c):
sampleCount += 1
print(sampleCount)
encode(720,1280)
Result: 921600
The same is your expectation?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论