无法在Python的双重循环中计算迭代次数。

huangapple go评论66阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2020年1月6日 22:03:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定