1

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

主要问题很容易解释:我试图在第一个循环中访问不存在的索引,导致索引超出范围错误。我尝试先评估有效性,但没有运气。对于熟悉 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作为计数器,一切都按预期工作!该列表将是一个基本列表,非常简单;)

4

3 回答 3

2
groups[count]

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

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

于 2013-11-12T22:50:05.600 回答
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))。您还可以用列表理解替换内部循环

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
于 2013-11-12T23:12:01.520 回答
0

正确的第 11 行来自:

   if i not in groups[count]

至:

   if i not in groups:
于 2013-11-12T22:47:28.557 回答