Delete values from a list of lists

1

How can I eliminate the double values that appear in some lists?

def selection_and_reproduction(poblacionNueva):

puntuados = [ [calcularFitness(i), i] for i in poblacionNueva] #Calcula el fitness de cada individuo, y lo guarda en pares ordenados de la forma (5 , [1,2,1,1,4,1,8,9,4,1])
def takeSecond(puntuados):
    return puntuados[0]
puntuados = sorted(puntuados, key=takeSecond) 

print("Puntuados:\n%s"%(puntuados))
poblacionNueva = puntuados
selected =  puntuados[(len(puntuados)-indAReproducir):] 

lista = list(range(1, largo))
print("LISTA:\n%s"%(lista))
random.shuffle(lista)
print("LISTA ALEATORIA:\n%s"%(lista))
for i in lista: 
    i = i-1
    punto =lista[i] 

    padre = random.sample(selected, 2)

    print("Punto:%s"%(punto))
    print("Padre:%s"%(padre))

    poblacionNueva[i][:punto] = (padre[0])[1][:punto] 
    print("Primera parte:%s"%(poblacionNueva[i][:punto]))

    poblacionNueva[i][punto:] = (padre[1])[1][punto:]
    print("Segunda parte:%s"%(poblacionNueva[i][punto:]))

    i = poblacionNueva[i][:punto] + poblacionNueva[i][punto:]
    print("Individuo Generado:%s"%(i))
    print("Fitness de I:%s"%(calcularFitness(i)))
    print("Individuo Generado CON FITNESS:%s"%(i))

poblacionNueva = [i for i in poblacionNueva]
print("Poblacion Nueva FINAL:\n%s"%(poblacionNueva))
return poblacionNueva 

It would be necessary to remove the value of the last lists from NewPopulation :

EDIT 1: I have used both Del and pop. It eliminates the value but it leaves me the empty space of the first list in the last two that I need to eliminate it too.

    
asked by Carlos Lozano 04.08.2017 в 18:36
source

3 answers

1

for the treatment of "lists" in python, there is a "pop ()" function, which removes an element according to its index (list location).

for example:

lista = [4,5,1]
lista.pop(0)

will remove the first element from the list, resulting in [5,1] (in python, the first index of the lists is 0 [zero])

lista = [4,5,1]
lista.pop(1)

will remove the second element from the list, resulting in [4,1]

I hope it serves you

    
answered by 01.06.2018 в 20:33
0

If you do not have excessively large lists, instead of deleting you can filter and create new versions of the two lists in the following way:

# Vamos a eliminar el elemento [4, 5, 3, 0, 4] que está en ambas listas
lista1 = [[5, 0, 3, 7, 3], [4, 5, 3, 0, 4]]           # poblacionNueva
lista2 = [[0, [4, 5, 3, 0, 4]], [2, [9, 6, 7, 7, 9]]] # puntuados

lista1new = [e for e in lista1 if e not in [e[1] for e in lista2]]
lista2new = [e for e in lista2 if e[1] not in lista1]

print(lista1new)
print(lista2new)
[[5, 0, 3, 7, 3]]
[[2, [9, 6, 7, 7, 9]]]

Basically we filtered the elements of the first list that were in the second one, we have to do a second comprehension operation, because the second list has a structure different from the type [a, [b, c, d, e, f] and then we simply do the same with the elements of the second list.

    
answered by 04.08.2017 в 20:18
0

you just have to do this:

    lista = [[1,2,3],[4,5],[6,7,8]]
    lista = list[1:-1]
    #eliminaria el primer y ultimo valor
    [[4,5]]

    lista2=[1,2,3,4,5]
    lista2 = [1:-1]
#eliminaria el primer y ultimo valor
    [2,3,4]

reference:
slicing python

    
answered by 09.11.2018 в 17:28