Home > Back-end >  Why does the code below return an empty list?
Why does the code below return an empty list?

Time:11-22

def outlier(*args):
    outlist=[]
    def median(args1):
        if(len(args1)%2==1):
            return list(sorted(args1))[int((len(args1)/2)-0.5)]
        else:
            return (list(sorted(args1))[int(len(args1)/2)] list(sorted(args1))[int(len(args1)/2)-1])/2
    def fmax(args2):
        sortargs=sorted(args2)
        return sortargs[-1]
    def fmin(args3):
        sortargs=sorted(args3)
        return sortargs[0]
    q1=median(list(range(fmin(args),floor(median(args)) 1)))
    q3=median(list(range(floor(median(args)),fmax(args) 1)))
    for i in args:
        if(i<(q1-1.5*(q3-q1)) or i>(q3 1.5*(q3-q1)*(q3-q1))):
            outlist.append(i)
    return outlist

print(outlier(1,2,3,4,5,6,7,8,9,10,100000000))

I have tried to get the outlier values of a list in Python , but everytime I try it ,it returns an empty list or throws an error.

CodePudding user response:

If the list returns empty, the reason is that neither parts of your if condition is met and so nothing is appended to the list:

if(i<(q1-1.5*(q3-q1)) or i>(q3 1.5*(q3-q1)*(q3-q1))):   # never met

If you are not adverse to useing bigger module, you could calculate the quartiles using numpy, see

or use this answer that gives you a function for manual calculation:


BTW:

  • sorted() returns a list so list(sorted(..)) is redundant
  • while smaller functions are nice, multiple times sorting the same data is not efficient - sort it once - to get the min/max of a list use:
  • all of your calculations need sorted input - you may as well sort it once on top and use the sorted list as inputs further down

You can also get the min and max in one sorting:

def minmax(data):
    if len(data) < 2: 
        raise ValueError("Must be iterable of len 2 or more")
    srt = sorted(data)

    return srt[0], srt[-1]

instead of

def fmax(args2):
    sortargs=sorted(args2)
    return sortargs[-1]
def fmin(args3):
    sortargs=sorted(args3)
    return sortargs[0]
  • Related