Home > Back-end >  Making a list of lists in Python
Making a list of lists in Python

Time:12-03

I'm tryin to create a list that is made it with another list: [[1,2,3,4] [5,6,7,8]] The thing here is that when I append with np.append() a list to another this is nesting instead of maintaining a list separate from each other, like this: [[1,2,3,4,5,6,7,8]] Here is my code:

    class poblacion:

        def generar(self):
            poblacion_p = np.random.randint(0,4,(10,10))
            print (poblacion_p)
            self.individuos(poblacion_p)

        def individuos(self, poblacion_p):
            individuo1=poblacion_p[0]
            individuo2=poblacion_p[1]
            individuo3=poblacion_p[2]
            individuo4=poblacion_p[3]
            individuo5=poblacion_p[4]
            individuo6=poblacion_p[5]
            individuo7=poblacion_p[6]
            individuo8=poblacion_p[7]
            individuo9=poblacion_p[8]
            individuo10=poblacion_p[9]
            aptitudes = [1,3,5,7,9,11,12,14,19,20]
            nueva_g=[]
            aptitud = random.choices(aptitudes, weights=(5,15,25,35,45,63,65,67,75,80), k = 10)
            for c in aptitud:
                if (c == 20):
                    nueva_g = np.append(nueva_g, individuo1)
                elif (c == 19):
                    nueva_g = np.append(nueva_g, individuo8)
                elif (c == 14):
                    nueva_g = np.append(nueva_g, individuo2)
                elif (c == 12):
                    nueva_g = np.append(nueva_g, individuo6)
                elif (c == 11):
                    nueva_g = np.append(nueva_g, individuo3)
                elif (c == 9):
                    nueva_g = np.append(nueva_g, individuo9)
                elif (c == 7):
                    nueva_g = np.append(nueva_g, individuo4)
                elif (c == 5):
                    nueva_g = np.append(nueva_g, individuo7)
                elif (c == 3):
                    nueva_g = np.append(nueva_g, individuo5)
                elif (c == 1):
                    nueva_g = np.append(nueva_g, individuo10)
            print(aptitud)
            print(nueva_g)

When I run the code the list of poblacion_p is in the form that I want:

[[3 2 1 1 0 0 1 3 0 0], 
[2 2 2 0 0 2 0 1 1 3], 
[3 3 0 0 1 3 3 1 1 3], 
[3 2 3 3 3 0 1 1 3 0], 
[3 1 2 0 3 2 2 0 0 1], 
[2 2 3 3 3 2 1 1 0 0],  
[2 3 2 0 3 1 1 3 2 0], 
[1 3 3 3 0 2 0 1 2 2], 
[3 1 1 2 3 1 3 3 3 2], 
[3 0 3 1 3 3 1 1 0 0]]

But when I try to append the rows that randomly selects the program in a new list happens this:

[3. 2. 1. 1. 0. 0. 1. 3. 0. 0. 2. 2. 3. 3. 3. 2. 1. 1. 0. 0. 2. 2. 2. 0. 0. 2. 0. 1. 1. 3. 3. 3. 0. 0. 1. 3. 3. 1. 1. 3. 2. 3. 2. 0. 3. 1. 1. 3. 2. 0. 3. 1. 1. 2. 3. 1. 3. 3. 3. 2. 2. 2. 3. 3. 3. 2. 1. 1. 0. 0. 2. 2. 2. 0. 0. 2. 0. 1. 1. 3. 2. 2. 3. 3. 3. 2. 1. 1. 0. 0. 1. 3. 3. 3. 0. 2. 0. 1. 2. 2.]

Does anyone know how to keep the lists separate from each other?

CodePudding user response:

You should just use append() method of list object like this:

for c in aptitud:
        if (c == 20):
            nueva_g.append(individuo1)
        elif (c == 19):
            nueva_g.append(individuo8)
        elif (c == 14):
            nueva_g.append(individuo2)

EDIT

As per the suggestion, if you wanted to rework the code you could use a dictionary as a map for this kind of problem. Something like this would work well.

MAP = {20: 0,
       19: 7,
       14: 1}

for c in aptid:
    nueva_g.append(poblacion_p[MAP[c]])

This only caters for the few examples I've given above, and I'd also probably restructure so the data is in a more logical order, but maybe there is a reason for that as it's being run in a for loop. This is untested but the general idea is sound.

You could also wrap the dictionary method in a switch function. Or as of Python 3.10 you could use the inbuilt switch statement.

https://docs.python.org/3/whatsnew/3.10.html

  • Related