import random def print_statistics(a_list): new_list = sorted(a_list) #create a sorted version of the list #a_list.sort() #alternative method, useful if memory is sparse and we don't need the original list #find the mean print "Mean = " float(sum(new_list) / len(new_list)) #find the median print "\nMedian = " midpt = int(len(new_list) / 2) if len(new_list) % 2 == 0: print (new_list[midpt] + new_list[midpt + 1]) / 2 else: print new_list[midpt] #find the mode md = 0 #stores the mode from the list md_count = 0 #stores the number of times md appears in our list for i in new_list: if new_list.count(new_list[i]) > md_count: #first check if this item appears more than the last suspect md = new_list[i] md_count = new_list.count(new_list[i]) else if new_list.count(new_list[i]) == md_count: #we may have several modes, this checks for them md.append(new_list[i]) print "\nMode = " md return l = 100 while i < l: tst_list.append(random.randint(1, 10)) print tst_list print_statistics(tst_list)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question