在尝试执行总计语句时,将索引脱离范围错误

发布于 2025-01-18 18:00:37 字数 244 浏览 3 评论 0原文

我正在尝试执行多个总计语句,但它一直说索引超出范围。 这是代码部分:

for m in range(len(mo)):
  for o in range(len(mag)):
    if mag[o] == 0 and mo[m] ==1 :
        countfujita[m] = countfujita[m] + 1

我试图将总数放入列表中,如下所示: 计数藤田 = [0,0,0,0,0,0]

I am trying to do a multiple totaling statements but it keeps saying index out of range.
Here is the section of code:

for m in range(len(mo)):
  for o in range(len(mag)):
    if mag[o] == 0 and mo[m] ==1 :
        countfujita[m] = countfujita[m] + 1

and I am trying to get the totals into list a list such as this:
countfujita = [0,0,0,0,0,0]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇纵 2025-01-25 18:00:37

我怀疑这是因为当您不需要时,您正在mag mo 中的每个项目。让我知道以下问题是否解决了您的问题:(

for m in range(len(mo)):
  if mag[m] == 0 and mo[m] ==1 :
      countfujita[m] = countfujita[m] + 1

这假定len(mag)= len(mo)

I suspect this is because you are looping over mag for every item in mo when this is not what you want. Let me know if the following fixes your issue:

for m in range(len(mo)):
  if mag[m] == 0 and mo[m] ==1 :
      countfujita[m] = countfujita[m] + 1

(this assumes that len(mag) = len(mo))

毁我热情 2025-01-25 18:00:37

为了使您的代码成功运行,您需要确保countfujita至少mo列表一样长。

以下是一种可靠的方法:

mo = [1, 0, 3]
mag = [3, 0, 1, 11]
# construct a list of the same length as *mo* and fill with zeroes
countfujita = [0] * len(mo)

for m in range(len(mo)):
    for o in range(len(mag)):
        if mag[o] == 0 and mo[m] == 1:
            countfujita[m] += 1

print(countfujita)

输出:

[1, 0, 0]

In order for your code to run successfully you need to ensure that countfujita is at least as long as the mo list.

The following would be a robust approach:

mo = [1, 0, 3]
mag = [3, 0, 1, 11]
# construct a list of the same length as *mo* and fill with zeroes
countfujita = [0] * len(mo)

for m in range(len(mo)):
    for o in range(len(mag)):
        if mag[o] == 0 and mo[m] == 1:
            countfujita[m] += 1

print(countfujita)

Output:

[1, 0, 0]
风柔一江水 2025-01-25 18:00:37

嵌套循环内部的多个列表易用。通常,我们希望尽可能利用Python内置模块。

例如(从lancelot du lac定义的示例开始),我们可以使用itertools.product生成momag的所有组合。 枚举为我们提供了与mo元素相对应的索引:

from itertools import product

for (imo, xmo), xmag in product(enumerate(mo), mag):
    if (xmo, xmag) == (1, 0):
        countfujita[imo] += 1

为了进一步推动此索引在所有mo索引中,然后计数。这将导致计数器对象计数器({0:1})对象,类似于dict,它可能根据您对countfujita 以后:

from itertools import product
from collections import Counter

Counter([imo for (imo, xmo), xmag 
         in product(enumerate(mo), mag) 
         if (xmo, xmag) == (1, 0)])

# Counter({0: 1})

Indexing multiple lists inside nested loops is error-prone. In general we want to leverage the python built-in modules as much as possible.

For example (starting from the example defined by Lancelot du Lac) we can use itertools.product to generate all combinations of mo and mag. enumerate gives us the index corresponding to the element of mo:

from itertools import product

for (imo, xmo), xmag in product(enumerate(mo), mag):
    if (xmo, xmag) == (1, 0):
        countfujita[imo] += 1

To push this even further, we can combine this with Counter to first generate a list of all mo indices and then count. This results in a counter object Counter({0: 1}) object, similar to a dict, which might or might not be appropriate depending on what you do with countfujita later on:

from itertools import product
from collections import Counter

Counter([imo for (imo, xmo), xmag 
         in product(enumerate(mo), mag) 
         if (xmo, xmag) == (1, 0)])

# Counter({0: 1})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文