List empty after python loop

Asked

Viewed 248 times

0

Hello, I have the following code that does this: It will traverse a vector that has a certain set of elements (residuo) with the same ID ([1][1][1][2][2] for example) and then store all elements of the same ID in separate vectors and then take these vectors(atomosDoAnel) of elements and will store in a single vector (aneiRes). The question is as follows, the vectors are stored correctly and if I print every time the loop runs, it (which is the final vector) prints correctly. However, if I print the ringRes before Return (outside the loop) it prints empty. Does anyone know why? The two vectors (atomsDoAnel and aneisRes are of the class whose function is declared) Note: I am beginner in python so sorry for anything strange in the code and any hint to improve will be very welcome. Thank you

def preencheAnel(self,  residuo, tamanhoVetRes):

    i = 1
    cont = 0
    aux = 0
    totalSub = 0         

    #verifica qual a quantidade de elementos iguais há em um conjunto pois é a mesma quantidade para todos os outros conjuntos
    aux = residuo[cont][4]
    while (aux == residuo[i][4]) & (i < tamanhoVetRes):
        cont = cont + 1
        i = i + 1
        aux = residuo[cont][4]

    #armazena em um vetor todos os elementos de um mesmo grupo num vetor e depois armazena esse em outro vetor
    cont = cont+1
    aux = 0
    i = 1
    totalSub = tamanhoVetRes / cont
    while i <= totalSub:
        while aux < (cont*i):
            self.atomosDoAnel.append(residuo[aux])
            aux = aux + 1                       
        print(i, aux)
        i = i+1
        cont = cont + i +1
        self.aneisRes.append(self.atomosDoAnel)
        print(self.aneisRes,"\n") #Imprime
        del self.atomosDoAnel[:]    

    print(self.aneisRes,"\n") #Não Imprime
    return(self.aneisRes)   
  • I’m not sure and I didn’t test it, but I think the problem is you’re erasing (del self.atomosDoAnel[:] ) which is apparently another object but in reality is not. http://answall.com/questions/187820/criar-umac%C3%B3pia-de-lista-em-python-de-forma-que-sejam-independentes/187822#187822 . In other words, there is actually only one copy of the elements inside atomosDoAnel when these are erased the ones that are within another variable (but are the same) are also. Try deleting that line del self.atom.. and see if it already prints

1 answer

0

The problem is in the self.atomsDoAnel[:], aux achieves the value greater than (conti) and loop while aux < (conti) no longer runs, self.aneisRes.append(self.atomosDoAnel) continues adding self.atomosDoAnel which is an empty list. To fix this you need to add aux = 0 inside the loop while i <= totalSub

 while i <= totalSub:
        while aux < (cont*i):
            self.atomosDoAnel.append(residuo[aux])
            aux = aux + 1                       
        print(i, aux)
        i = i+1
        cont = cont + i +1
        self.aneisRes.append(self.atomosDoAnel)
        print(self.aneisRes,"\n") #Imprime
        del self.atomosDoAnel[:] 
        aux = 0

or else think of a different algorithm that does not depend on the self.atomosDoAnel[:]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.