1

I am trying to make a median program. This is my code.

def median(list=[]):
    x = 0
    list = sorted(list)
    if len(list) % 2 == 0:
        b = int((len(list)/2))
        print b
        c = int(len(list)/2)
        x = [list[b-1],list[c]]
        print x
        x = float((x[0]+x[1])/2)
        print str(x)
    else:
        print list[((len(list)//2))]

When I run it, I get this

>>> median([1,2,3,4])
2
[2, 3]
2.0

What is happening?

Please note: I do not want any advice on ways to do it better, I just want help.

Serjik
  • 10,543
  • 8
  • 61
  • 70
Midataur
  • 21
  • 4

3 Answers3

1

Python 3.4 has statistics.median

Here is a example of how to use it

import statistics

values = [1, 2, 3, 6, 8]

print(statistics.median(values))

If you don't use python version 3.4 you could always right a function like this one i have included

def median(lst):
    lst = sorted(lst)
    if len(lst) < 1:
            return None
    if len(lst) %2 == 1:
           return lst[((len(lst)+1)/2)-1]
    else:
            return float(sum(lst[(len(lst)/2)-1:(len(lst)/2)+1]))/2.0

while these may be the more pythonic way to do it, if you would like your specific function, you can do it like this

def median(list=[]):
    x = 0
    list = sorted(list)
    if len(list) % 2 == 0:
        b = int((len(list)/2.0))
        print b
         c = int(len(list)/2.0)
        x = [list[b-1],list[c]]
        print x
        x = float((x[0]+x[1])/2.0)
        print str(x)
    else:
        print list[((len(list)//2.0))]

median([1,2,3,4])
Ryan
  • 1,972
  • 2
  • 23
  • 36
1

Try this, look for 2.0 instead of 2 on division

def median(list=[]):
    x = 0
    list = sorted(list)
    if len(list) % 2 == 0:
        b = int((len(list)/2.0))
        print b
        c = int(len(list)/2.0)
        x = [list[b-1],list[c]]
        print x
        x = float((x[0]+x[1])/2.0)
        print str(x)
    else:
        print list[((len(list)//2))]
Serjik
  • 10,543
  • 8
  • 61
  • 70
0

You should cast the elements of x to a float before taking the average:

def median(list=[]):
    x = 0
    list = sorted(list)
    if len(list) % 2 == 0:
        b = int(len(list)/2)
        print b
        c = int(len(list)/2)
        x = [list[b-1],list[c]]
        print x
        x = (float(x[0])+float(x[1]))/2
        print str(x)
    else:
        print list[((len(list)//2))]

median([1,2,3,4])

In the original you had x = float((x[0]+x[1])/2). So that's adding 2 integers and dividing them. If this is python 2 the you run into the integer division problem (I don't believe this will happen in python 3 because / is float division by default). You fix this by explicitly telling python that your values are floats (as I did above), or you can from __future__ import division and just use the default float division from python 3.

Also, b and c are both set to the same value so you don't need to store them both separately. Just use either b or c when calculating your list x

Simon
  • 9,762
  • 15
  • 62
  • 119