Connecting Tech Pros Worldwide Help | Site Map

How do you find the median of a list?

Newbie
 
Join Date: Nov 2008
Posts: 2
#1: Nov 9 '08
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:
Expand|Select|Wrap|Line Numbers
  1. mark = 0
  2. count = 0
  3. yourmarks =[]
  4. while True:
  5.     marks = input("Enter student marks here[-1 to stop]")
  6.     yourmarks.append(marks)
  7.     count += 1
  8.     if marks == -1:
  9.         break
  10. print "Your highest mark is",max(yourmarks)
  11. del yourmarks[-1]
  12. print "Your lowest mark is", min(yourmarks)
  13.  
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#2: Nov 9 '08

re: How do you find the median of a list?


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.
Newbie
 
Join Date: Nov 2008
Posts: 2
#3: Nov 9 '08

re: How do you find the median of a list?


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?
boxfish's Avatar
Expert
 
Join Date: Mar 2008
Location: California
Posts: 478
#4: Nov 10 '08

re: How do you find the median of a list?


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?
Expand|Select|Wrap|Line Numbers
  1. if len(yourmarks) % 2 == 0:
  2.     print "Division by two leaves no remainder,"
  3.     print "so list length is even."
  4. else:
  5.     print "There was a remainder,"
  6.     print "So the list contains an odd number of items."
  7.  
Newbie
 
Join Date: Jun 2007
Posts: 15
#5: Nov 13 '08

re: How do you find the median of a list?


You want to find the median of a list?

Expand|Select|Wrap|Line Numbers
  1. x=[1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54]
  2. median=sorted(x)[len(x)/2]
  3.  
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#6: Nov 13 '08

re: How do you find the median of a list?


Quote:

Originally Posted by rogerlew

You want to find the median of a list?

Expand|Select|Wrap|Line Numbers
  1. x=[1,2,5,2,3,763,234,23,1,234,21,3,2134,23,54]
  2. median=sorted(x)[len(x)/2]
  3.  

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.
Expand|Select|Wrap|Line Numbers
  1. def median(s):
  2.     i = len(s)
  3.     if not i%2:
  4.         return (s[(i/2)-1]+s[i/2])/2.0
  5.     return s[i/2]
Newbie
 
Join Date: Nov 2009
Posts: 1
#7: 2 Weeks Ago

re: How do you find the median of a list?


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
Expand|Select|Wrap|Line Numbers
  1. def median (y):
  2.     z = len(y)
  3.     if not z%2:
  4.         return (y[(z/2)-1] + y[z/2]) / 2
  5.     return y[z/2]
what am i doing wrong?
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#8: 2 Weeks Ago

re: How do you find the median of a list?


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.
Expand|Select|Wrap|Line Numbers
  1. >>> def median(y):
  2. ...     z = len(y)
  3. ...     if not z%2:
  4. ...         return (y[(z/2)-1] + y[z/2]) / 2
  5. ...     else:
  6. ...         return y[z/2]
  7. ...        
  8. >>> median
  9. <function median at 0x0109CEF0>
  10. >>> y=[6,3,9,1,2,8,9,12,34,12]
  11. >>> y.sort()
  12. >>> median(y)
  13. 8
  14. >>> y
  15. [1, 2, 3, 6, 8, 9, 9, 12, 12, 34]
  16. >>> 
Reply