473,473 Members | 2,031 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How do you find the median of a list?

2 New Member
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.  
Nov 9 '08 #1
8 34256
boxfish
469 Recognized Expert Contributor
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.
Nov 9 '08 #2
vince1266
2 New Member
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?
Nov 9 '08 #3
boxfish
469 Recognized Expert Contributor
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.  
Nov 10 '08 #4
rogerlew
15 New Member
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.  
Nov 13 '08 #5
bvdet
2,851 Recognized Expert Moderator Specialist
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]
Nov 13 '08 #6
pythonuser18
1 New Member
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?
Nov 6 '09 #7
bvdet
2,851 Recognized Expert Moderator Specialist
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. >>> 
Nov 6 '09 #8
bferguson94
11 New Member
def main():
myList=[]
choice = 0
while choice !='4':
choice= raw_input('1.add 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(myList)
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("input new number:")
try:
num = int(num)
if num < 0:
raise ValueError
break
except:
print"invalid input"


myList.append(num)

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
Feb 26 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
2
by: michael way | last post by:
I read the follow query about calculating median posted by Daivd Porta on 10/8/03. CREATE TABLE SomeValues (keyx CHAR(1) PRIMARY KEY, valuex INTEGER NOT NULL) INSERT INTO SomeValues VALUES...
2
by: Hugo L. | last post by:
I really don't know how to calculate the median. Can anybody help me?
2
by: Bob | last post by:
I have been looking at the code for MedianFind(pDte As String) from the following thread from UtterAccess.com: "Finding Median average grouped by field" I have been able to get it to run using...
8
by: nick.vitone | last post by:
Hi, I'm somewhat of a novice at Access, and I have no experience programming whatsoever. I'm attempting to calculate the statistical median in a query. I need to "Group by" one column and find...
3
by: Scott | last post by:
I need to take the median from a field of records in a report. Can someone shed the light how to do it. Thanks, Scott
3
by: mehwishobaid | last post by:
i dont know wat is wrong with my code. when i compile. i get the error saying line 29: error: expression must have pointer-to-object type #include <iostream> using namespace std; #include...
7
by: tomshorts07 | last post by:
hi everyone! i was wondering if anyone had any ideas on how to go about coding a method that would determine the median of a list of numbers just to clarify- a median is the 'middle value' when...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.