简体   繁体   中英

Why is the code below return an empty list.I could not understand the reason of it so if someone can help me about that , that will make me happy

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, but I want neither of these so if you are able to solve my problem and solve, that will make me happy.Thanks for reading if you need more information to solve the problem, you may ask.

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

Why is that so? Well - q1 and q3 are calculated here:

 q1 = median(list(range(fmin(args),floor(median(args))+1))) q3 = median(list(range(floor(median(args)),fmax(args)+1)))

and both are range s converted to list s, stuffed into your median function that returns list s as well.

  • What do you think the result of q3-q1 (list - list) is?
  • What do you think the result of 1.5*(q3-q1)*(q3-q1) (1.5 times the square of list-list) should be?

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:
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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM