How do you find the median of a list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vince1266
    New Member
    • Nov 2008
    • 2

    How do you find the median of a list?

    I have a little bit of trouble finding the median in a list. I am trying to ask the user what his/her marks are and i am trying to find the median. I have a little bit of trouble because the user could be inputting either an odd or an even number of marks but i don't know how to ask him/her. it would be great if someone could help me. This is what i have so far:
    Code:
    mark = 0
    count = 0
    yourmarks =[]
    while True:
        marks = input("Enter student marks here[-1 to stop]")
        yourmarks.append(marks)
        count += 1
        if marks == -1:
            break
    print "Your highest mark is",max(yourmarks)
    del yourmarks[-1]
    print "Your lowest mark is", min(yourmarks)
    Last edited by numberwhun; Nov 10 '08, 02:56 PM. Reason: Please use code tags
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    A number is even if dividing it by two leaves no remainder. To get the remainder from a division, you use the modulus operator, %. So you would use
    len(yourmarks) % 2
    if that is equal to zero, then there are an even number of items in youmarks.
    By the way, it would be helpful if you used code tags around your code. Put [CODE] before the code and [/CODE] after it, so it shows up in a code box and the indentation isn't wrecked. Thanks.
    I hope this is helpful.

    Comment

    • vince1266
      New Member
      • Nov 2008
      • 2

      #3
      thanks for the help and advice but i still dont seem to understand what you are talking about.. could you show me exactly how you would do it?

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        You know what len does, right?
        8 % 4 is equal to zero, because 8 is divisible by 4 with no remainder. But 11 % 4 is 3, because you get a remainder of 3 when you divide 11 by 4.
        Does the following example help?
        Code:
        if len(yourmarks) % 2 == 0:
            print "Division by two leaves no remainder,"
            print "so list length is even."
        else:
            print "There was a remainder,"
            print "So the list contains an odd number of items."

        Comment

        • rogerlew
          New Member
          • Jun 2007
          • 15

          #5
          You want to find the median of a list?

          Code:
          x=[1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54]
          median=sorted(x)[len(x)/2]

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by rogerlew
            You want to find the median of a list?

            Code:
            x=[1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54]
            median=sorted(x)[len(x)/2]
            That works great when the list length is an odd number. When it is an even number, you take the average of the two middle numbers in a sorted list.
            [code=Python]def median(s):
            i = len(s)
            if not i%2:
            return (s[(i/2)-1]+s[i/2])/2.0
            return s[i/2][/code]

            Comment

            • pythonuser18
              New Member
              • Nov 2009
              • 1

              #7
              Error

              This post is a year old so I don't know if I'll get any replies, but my median function doesn't work.... It yields <function median at 0x01D36F30> this is wut i used, just like what is posted
              Code:
              def median (y):
              	z = len(y)
              	if not z%2:
              		return (y[(z/2)-1] + y[z/2]) / 2
              	return y[z/2]
              what am i doing wrong?
              Last edited by bvdet; Nov 6 '09, 02:06 PM. Reason: Add code tags

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                You are not calling the function with an argument. You have another problem. If you have a list of integers and the length of the list is an even number, the function calculates the average of the two middle numbers. Dividing by the integer 2 results in integer division. In the example below, (8+9)/2 = 8 but the answer is 8.5.
                Code:
                >>> def median(y):
                ... 	z = len(y)
                ... 	if not z%2:
                ... 	    return (y[(z/2)-1] + y[z/2]) / 2
                ... 	else:
                ... 	    return y[z/2]
                ... 	   
                >>> median
                <function median at 0x0109CEF0>
                >>> y=[6,3,9,1,2,8,9,12,34,12]
                >>> y.sort()
                >>> median(y)
                8
                >>> y
                [1, 2, 3, 6, 8, 9, 9, 12, 12, 34]
                >>>

                Comment

                • bferguson94
                  New Member
                  • Feb 2010
                  • 11

                  #9
                  median for even num. HELP!!!

                  def main():
                  myList=[]
                  choice = 0
                  while choice !='4':
                  choice= raw_input('1.ad d number to list\n2.get mean \n3.get median \n4.quit')
                  if choice == '1':
                  addNum(myList)
                  if choice == '2':
                  getMean(myList)
                  if choice == '3':
                  getMedian(myLis t)
                  if int(choice) < 0:
                  print "invalid input"
                  if int(choice) > 4:
                  print "invalid input"
                  ##break
                  ##input error messages
                  def addNum(myList):
                  while True:
                  num = raw_input("inpu t new number:")
                  try:
                  num = int(num)
                  if num < 0:
                  raise ValueError
                  break
                  except:
                  print"invalid input"


                  myList.append(n um)

                  def getMean(myList) :
                  total = 0.0
                  for value in myList:
                  total = total + value
                  mean = total/len(myList)
                  print "the mean is:" , mean

                  def getMedian (myList):
                  index= 2
                  if len(myList)%2 == 0:
                  myMed = (myList[index] + myList[myMed-1])/2
                  else:
                  myList[len(myList)/2]
                  print myMed

                  Comment

                  Working...