Delete part of a set of tuples that form an array

3

Good, we are doing a small program in Python and we want to eliminate the first element of the different nested lists. The code is this:

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) 
poblacionNueva = puntuados

This is what we get as an output and we want to eliminate the first value of each tuple.

    
asked by Carlos Lozano 25.07.2017 в 17:31
source

1 answer

2

Well, let's see if I understood. You want to delete the first element of all the tuples in the array. Note that what you are showing is a list of lists, not a list of tuples. Remember:

>>> tupla = (10, 20, 30) # Paréntesis
>>> lista = [10, 20, 30] # Corchetes

If you have something like this (a list of lists):

[
    [1, [10, 20, 30, 40, 50]],
    [2, [100, 200, 300, 400, 500]]
]

And you want to extract the second element (as opposed to removing the first element) you can do it with comprehension lists:

>>> l
[[1, [10, 20, 30, 40, 50]], [2, [100, 200, 300, 400, 500]]]
>>> l2 = [x[1] for x in l]
>>> l2
[[10, 20, 30, 40, 50], [100, 200, 300, 400, 500]]

With this I am assuming that that is what you meant by "eliminating the first element of the different tuples that form an array".

In your case, I assume that the variable that contains the output you are showing is poblacionNueva , then you would have to extract the values from that list:

 >>> poblacionNueva = [x[1] for x in poblacionNueva]   

Or save it in a new variable to avoid losing the original data:

 >>> poblacionNueva2 = [x[1] for x in poblacionNueva]   
    
answered by 25.07.2017 / 17:43
source