[英]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.我有一个 function 可以从列表中删除重复项,如果删除了,则用另一个列表的另一个条目再次填充那些空白,其中包含所有可能的条目。
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".现在,我有一个 function 可以完成这项工作,但我不知道为什么,当我调用它时将完整列表不重复地存储在另一个列表中时,它显示为“无”。 But it works properly.但它工作正常。 This is the function:这是 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.如果我在 function 的末尾打印sinrep_AUX
(最终正确列表)的内容一切正常,问题出现在调用存储它的时候。
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在这里,第一行显示 print sinRep_AUX
from function elimina_Rep
的内容,最后一行显示preg_filt
的内容,我想这是存储 function 返回列表的列表,当我调用它时
['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正如我在其他帖子中看到的那样,我尝试将返回行从return sinRep_AUX
为return elimina_Rep(dondeborrar, cantidadNecesaria, fichcompleto)
,但它不起作用
You only return something when if sinRep == dondeborrar
fails and then during the loop if(sinRep == sinRep_AUX)
succeeds.你只在if sinRep == dondeborrar
失败时返回一些东西,然后在循环中if(sinRep == sinRep_AUX)
成功。
You can solve these problems by moving the return
statement to the end of the function.您可以通过将return
语句移动到 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.