嵌套for循环索引超出范围

时间:2013-11-12 22:44:18

标签: python list for-loop indexoutofboundsexception cinema-4d

我想出了一个相当微不足道的问题,但是因为我对蟒蛇很陌生,所以我会把头撞到我的桌子上一段时间。 (好痛)。虽然我认为解决问题更合乎逻辑...... 首先我要说我正在使用Python SDK for Cinema 4D,所以我不得不稍微更改以下代码。但这就是我想要做的事情并且在努力: 我正在尝试对一些多边形选择进行分组,这些选择是动态生成的(基于某些规则,而不是那么重要)。 以下是数学方法的工作原理: 这些选择基于岛屿(意味着有几个多边形相连)。 然后,必须将这些选择分组并放入我可以使用的列表中。 任何多边形都有自己的索引,所以这个应该相当简单,但就像我之前说的那样,我在那里很挣扎。

主要问题很容易解释:我试图在第一个循环中访问一个不存在的索引,导致索引超出范围错误。我首先尝试评估有效性,但没有运气。对于那些熟悉Cinema 4D + Python的人,如果有人想要,我会提供一些原始代码。到目前为止,这么糟糕。这是简化和改编的代码。

编辑:忘记提到导致错误的检查实际上应该只检查重复项,因此当前选择的数字将被跳过,因为它已被处理。由于计算量很大,这是必要的。

真的希望,任何人都可以让我朝着正确的方向前进,这段代码到目前为止是有道理的。 :)

def myFunc():

        sel = [0,1,5,12] # changes with every call of "myFunc", for example to [2,8,4,10,9,1], etc. - list alway differs in count of elements, can even be empty, groups are beeing built from these values
        all = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] # the whole set
        groups = [] # list to store indices-lists into
        indices = [] # list to store selected indices
        count = 0 # number of groups
        tmp = [] # temporary list to copy the indices list into before resetting

        for i in range(len(all)): # loop through values
            if i not in groups[count]: # that's the problematic one; this one actually should check whether "i" is already inside of any list inside the group list, error is simply that I'm trying to check a non existent value
                for index, selected in enumerate(sel): # loop through "sel" and return actual indices. "selected" determines, if "index" is selected. boolean.
                    if not selected: continue # pretty much self-explanatory
                    indices.append(index) # push selected indices to the list
                tmp = indices[:] # clone list
                groups.append(tmp) # push the previous generated list to another list to store groups into
                indices = [] # empty/reset indices-list
                count += 1 # increment count
        print groups    # debug
myFunc()

修改

添加第二个列表后,extend将填充,而不是append作为计数器,所有内容都按预期运行!该列表将是一个基本列表,非常简单;)

3 个答案:

答案 0 :(得分:2)

groups[count]

当你第一次调用它时,groups是一个空列表,count是0.你不能在组中的0点访问这个东西,因为那里什么都没有!

尝试制作 groups = []groups = [[]](即不是空列表,只列出空列表的列表)。

答案 1 :(得分:1)

我不确定您为什么要将空列表添加到组中。也许这样更好

if i not in groups[count]:

if not groups or i not in groups[count]:

如果您不打算将其用于其他任何目的,您也无需复制该列表。所以你可以替换

            tmp = indices[:] # clone list
            groups.append(tmp) # push the previous generated list to another list to store groups into
            indices = [] # empty/reset indices-list

            groups.append(indices) # push the previous generated list to another list to store groups into
            indices = [] # empty/reset indices-list

您甚至可以完全放弃count(您可以随时使用len(groups))。您还可以使用listcomprehension

替换内部循环
def myFunc():

    sel = [0,1,5,12] # changes with every call of "myFunc", for example to [2,8,4,10,9,1], etc. - list alway differs in count of elements, can even be empty, groups are beeing built from these values
    all = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] # the whole set
    groups = [] # list to store indices-lists into

    for i in range(len(all)): # loop through values
        if not groups or i not in groups[-1]: # look in the latest group
            indices = [idx for idx, selected in enumerate(sel) if selected]
            groups.append(indices) # push the previous generated list to another list to store groups into
    print groups    # debug

答案 2 :(得分:0)

正确的第11行:

   if i not in groups[count]

为:

   if i not in groups: