how to omit values in python

1

I try to get an array of averages from a list but there are some gaps then as a condition that I do not use that empty values

list = [1, "", 1,2,4, ""]

how to add an if wing instruction so that it does not use the NoneType and exit error

promedio = [sum(lista[:i+1])/float(i+1) for i in range(len(lista)-1)]

This is a mistake that marks me

promedio = [sum(lista[:i+1])/float(i+1) for i in range(len(lista)-1)]
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
    
asked by Luis Alberto Acosta 21.08.2017 в 22:13
source

1 answer

1

what could be done is the following.

  • Delete what you do not want by means of a filter
  • Perform the calculation of the average by applying a reduction and then dividing by the length of the list that has previously been purified.

         lista=[1,None,1,2,4,""]
         newLista=filter(lambda x:not x in ("",None),lista)
         print(reduce(lambda x,y:x+y,newLista)/len(newLista))
    
  • This code works OK for what you plan.

    Greetings.

        
    answered by 19.02.2018 в 17:23