Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Making a list of lists in Python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[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?

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading