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

Python function return 'None' list

I have a function that removes duplicates from a list, and if removed, fills again those blanks with another entry of another list with all possible entries.

Right now, I have a function which does this work, but I don`t know why, at time to store that complete list without duplicates in another list when I call it, it appears as "none". But it works properly.
This is the function:

def elimina_Rep(dondeborrar, cantidadNecesaria, fichcompleto):
    sinRep = []                                                 # Donde almacenaremos las NO repetidas
    sinRep_AUX = []                                             # Para borrar en la borrada, (varios ciclos de borrado)
    for elem in dondeborrar:                                    # Primera busqueda de elementos duplicados
        if(elem not in sinRep): sinRep.append(elem)             # Obtenemos una lista sin repeticiones
    if sinRep == dondeborrar : pass                             # Comprobamos que haya diferencia de cantidad entre ambas
    else:
        while(sinRep != sinRep_AUX):
            dif = (cantidadNecesaria - len(sinRep))             # Obtenemos cuantos elementos hemos borrado
            for i in range(0,dif):                              # Generamos tantas nuevas entradas como las que hemos borrado
                sinRep.append(random.choice(fichcompleto))      # Obtenemos una nueva lista completa (pero con posibles repeticiones)    
            for j in sinRep:                                    # Comprobamos que no haya repeticiones de la primera busqueda
                if(j not in sinRep_AUX):                        # Segunda busqueda de elementos duplicados  
                    sinRep_AUX.append(j)                        # Obtenemos una lista sin repeticiones sinRep_AUX desde la primera sin rep  
            if(sinRep == sinRep_AUX): 
                return sinRep_AUX
            else:
                sinRep = sinRep_AUX
                sinRep_AUX = []  

If I print at the end of the function the content of sinrep_AUX (final correct list) all is OK, the problem appears in the calling at time to store it.

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

def gen_Preg(asignatura, porcentaje):
    preguntas = []                                              # Creamos una lista vacia para alamcenar las preguntas
    porc = int((porcentaje/100)*totalpreguntas)                 # Obtenemos cuantas preguntas necesitamos de esa asignatura por %

    # Bucle for para obtener y escribir la 1a vez (con posibles duplicaciones)
    with open(asignatura, 'r') as aPreg:
        fichero = aPreg.read().strip().splitlines()             # Obtenemos el fichero sin posibles espacios
        for i in range(0,porc):
            preguntas.append(random.choice(fichero))

    random.shuffle(preguntas)                                   # Mezclamos las preguntas generadas
    preg_filt = elimina_Rep(preguntas, porc, fichero)           # Tenemos un archivo con preguntas sin repeticiones
    print(preg_filt)

Here, the first line shows the content of print sinRep_AUX from function elimina_Rep, and the last line, shows the content of preg_filt, that I suppose that is the list which store the returned list of the function when I call it

['Como te llamas?', 'Donde vives?', 'Como tomas apuntes?']
None

I’ve tried to change the return line from return sinRep_AUX to return elimina_Rep(dondeborrar, cantidadNecesaria, fichcompleto) as I saw in other posts, but it doesn’t work

>Solution :

You only return something when if sinRep == dondeborrar fails and then during the loop if(sinRep == sinRep_AUX) succeeds.

You can solve these problems by moving the return statement to the end of the function.

def elimina_Rep(dondeborrar, cantidadNecesaria, fichcompleto):
    sinRep = []                                                 # Donde almacenaremos las NO repetidas
    sinRep_AUX = []                                             # Para borrar en la borrada, (varios ciclos de borrado)
    for elem in dondeborrar:                                    # Primera busqueda de elementos duplicados
        if(elem not in sinRep): sinRep.append(elem)             # Obtenemos una lista sin repeticiones
    if sinRep == dondeborrar : pass                             # Comprobamos que haya diferencia de cantidad entre ambas
    else:
        while(sinRep != sinRep_AUX):
            dif = (cantidadNecesaria - len(sinRep))             # Obtenemos cuantos elementos hemos borrado
            for i in range(0,dif):                              # Generamos tantas nuevas entradas como las que hemos borrado
                sinRep.append(random.choice(fichcompleto))      # Obtenemos una nueva lista completa (pero con posibles repeticiones)    
            for j in sinRep:                                    # Comprobamos que no haya repeticiones de la primera busqueda
                if(j not in sinRep_AUX):                        # Segunda busqueda de elementos duplicados  
                    sinRep_AUX.append(j)                        # Obtenemos una lista sin repeticiones sinRep_AUX desde la primera sin rep  
            if(sinRep == sinRep_AUX): 
                break
            else:
                sinRep = sinRep_AUX
                sinRep_AUX = []  
    return sinRep_AUX
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