簡體   English   中英

嵌套for循環索引超出范圍

[英]Nested for loop index out of range

我想出了一個相當微不足道的問題,但是因為我對蟒蛇很陌生,所以我會把頭撞到我的桌子上一段時間。 (好痛)。 雖然我認為解決這個問題更合乎邏輯......首先我要說我正在使用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作為計數器,所有內容都按預期工作! 該列表將是一個基本列表,非常簡單;)

groups[count]

當你第一次調用它時,groups是一個空列表,count是0.你不能在組中的0點訪問那個東西,因為那里什么都沒有!

嘗試將groups = []設為groups = [[]] (即代替空列表,列表中只有空列表的列表)。

我不確定你為什么要將空列表添加到組中。 也許這更好

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

正確的第11行來自:

   if i not in groups[count]

至:

   if i not in groups:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM