Domanda

I'm coming up with a rather trivial problem, but since I'm quite new to python, I'm smashing my head to my desk for a while. (Hurts). Though I believe that's more a logical thing to solve... First I have to say that I'm using the Python SDK for Cinema 4D so I had to change the following code a bit. But here is what I was trying to do and struggling with: I'm trying to group some polygon selections, which are dynamically generated (based on some rules, not that important). Here's how it works the mathematical way: Those selections are based on islands (means, that there are several polygons connected). Then, those selections have to be grouped and put into a list that I can work with. Any polygon has its own index, so this one should be rather simple, but like I said before, I'm quite struggling there.

The main problem is easy to explain: I'm trying to access a non existent index in the first loop, resulting in an index out of range error. I tried evaluating the validity first, but no luck. For those who are familiar with Cinema 4D + Python, I will provide some of the original code if anybody wants that. So far, so bad. Here's the simplified and adapted code.

edit: Forgot to mention that the check which causes the error actually should only check for duplicates, so the current selected number will be skipped since it hal already been processed. This is necessary due to computing-heavy calculations.

Really hope, anybody can bump me in the right direction and this code makes sense so far. :)

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()

edit:

After adding a second list which will be filled by extend, not append that acts as counter, everything worked as expected! The list will be a basic list, pretty simple ;)

È stato utile?

Soluzione

groups[count]

When you first call this, groups is an empty list and count is 0. You can't access the thing at spot 0 in groups, because there is nothing there!

Try making groups = [] to groups = [[]] (i.e. instead of an empty list, a list of lists that only has an empty list).

Altri suggerimenti

I'm not sure why you'd want to add the empty list to groups. Perhaps this is better

if i not in groups[count]:

to

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

You also don't need to copy the list if you're not going to use it for anything else. So you can replace

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

with

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

You may even be able to drop count altogether (you can always use len(groups)). You can also replace the inner loop with a 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

correct line 11 from:

   if i not in groups[count]

to:

   if i not in groups:
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top