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

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

Can't count iterations on double for loop in python

问题

以下是翻译好的部分:

  1. 我在实现一个用于计数双重循环迭代的计数器时遇到了问题
  2. 我的代码如下
  3. def encode(mat, l, c, mode, m):
  4. sampleCount = 0
  5. for i in range(l):
  6. for j in range(c):
  7. sampleCount += 1
  8. print(sampleCount)
  9. 我的程序使用分别为720360l值以及分别为1280640c值调用此函数我预期的是sampleCount的值分别为921600230400然而它却打印出了1279639
  10. 此外当我像这样测试打印ij
  11. for i in range(l):
  12. print(i)
  13. for j in range(c):
  14. print(j)
  15. 我得到的结果是程序首先打印出所有i0l-1然后才打印出j0c-1
  16. 有人可以告诉我可能出了什么问题吗提前谢谢
  17. 编辑粘贴代码时没有缩进
  18. 编辑2尝试在sampleCount += 1之后注释掉所有内容在这种情况下我得到了预期的结果如果取消注释下面的两行代码它会继续正常工作然而当我尝试取消注释超过3行的代码时它会再次出现问题简而言之当代码像这样时它可以工作
  19. def encode2(mat, l, c, mode, m):
  20. sampleCount = 0
  21. for i in range(l):
  22. for j in range(c):
  23. sampleCount += 1
  24. a = 0
  25. b = 0
  26. # c = 0
  27. # x = 0
  28. # if (i == 0 & j == 0):
  29. # a = 0
  30. # b = 0
  31. # c = 0
  32. ... ...
  33. 并且如果代码像这样时它会再次出现问题
  34. def encode2(mat, l, c, mode, m):
  35. sampleCount = 0
  36. for i in range(l):
  37. for j in range(c):
  38. sampleCount += 1
  39. a = 0
  40. b = 0
  41. c = 0
  42. # x = 0
  43. # if (i == 0 & j == 0):
  44. # a = 0
  45. # b = 0
  46. # c = 0
  47. ... ...

希望这有助于解决你的问题。

英文:

I am having trouble implementing a counter to count over iterations on a double for loop.

My code is the following:

  1. def encode(mat,l,c,mode,m):
  2. sampleCount = 0
  3. for i in range(l):
  4. for j in range(c):
  5. sampleCount += 1
  6. 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:

  1. for i in range(l):
  2. print(i)
  3. for j in range(c):
  4. 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:

  1. def encode2(mat,l,c,mode,m):
  2. sampleCount = 0
  3. for i in range(l):
  4. for j in range(c):
  5. sampleCount += 1
  6. a = 0
  7. b = 0
  8. # c = 0
  9. # x = 0
  10. # if (i == 0 & j == 0):
  11. # a = 0
  12. # b = 0
  13. # c = 0
  14. ... ...

And misbehaves again if the code is like this:

  1. def encode2(mat,l,c,mode,m):
  2. sampleCount = 0
  3. for i in range(l):
  4. for j in range(c):
  5. sampleCount += 1
  6. a = 0
  7. b = 0
  8. c = 0
  9. # x = 0
  10. # if (i == 0 & j == 0):
  11. # a = 0
  12. # b = 0
  13. # c = 0
  14. ... ...

答案1

得分: 1

以下是翻译好的代码部分:

  1. 我运行相同的代码时得到了以下结果
  2. def encode(l,c):
  3. sampleCount = 0
  4. for i in range(l):
  5. for j in range(c):
  6. sampleCount += 1
  7. print(sampleCount)
  8. encode(360,640)

结果:230400

  1. def encode(l,c):
  2. sampleCount = 0
  3. for i in range(l):
  4. for j in range(c):
  5. sampleCount += 1
  6. print(sampleCount)
  7. encode(720,1280)

结果:921600

您的期望也是这样吗?

英文:

I got the following result when I ran the same code:

  1. def encode(l,c):
  2. sampleCount = 0
  3. for i in range(l):
  4. for j in range(c):
  5. sampleCount += 1
  6. print(sampleCount)
  7. encode(360,640)

Result: 230400

  1. def encode(l,c):
  2. sampleCount = 0
  3. for i in range(l):
  4. for j in range(c):
  5. sampleCount += 1
  6. print(sampleCount)
  7. 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:

确定