数据统计值的计算+PYTHON

#基本统计值得计算
#CalStatisticsV1.py
def getnum():
nums=[]
inumstr=input("请输入数字(回车退出):")
while inumstr !="":
nums.append(eval(inumstr))
inumstr=input("请输入数字(回车退出):")
return nums

def mean(numbers):
s=0.0
for i in numbers:
s=s+i
return s/len(numbers)

def dev(numbers,mean):#计算方差
sdev = 0.0
for i in numbers:
sdev = sdev+(i-mean)**2
return pow(sdev/len(numbers)-1,0.5)

#sorted 可以直接给列表排序
def median(numbers):#计算中位数
sorted(numbers)
size=len(numbers)
if size%2==0:
med=(numbers[size//2-1]+numbers[size//20])/2
else:
med=numbers[size//2]
return med

n=getnum()
m=mean(n)
print("平均值{},方差:{:.2}.".format(m,dev(n,m)))





#计算中位数
def median(numbers):
sorted(numbers)
size=len(numbers)
if size % 2==0:
med=(numbers[size//2-1]+numbers[size//2])/2
else:
med=numbers[size//2]
return med
def getnums():
n=[]
n1=input()
while n1!="":
n.append(eval(n1))
n1=input()
return n

m=getnums()
print(sorted(m),end=" ")
print("这组数据的中位数是%d\n"%median(m))
print("这组数据的中位数是{}".format(median(m)))

'''
numbers=[9,2,3]
for i in range (len(numbers) - 1):
for j in range (len(numbers) - i - 1):
if (numbers[j] > numbers[j + 1]):
numbers[j + 1], numbers[j] = numbers[j], numbers[j + 1]
for k in range (len(numbers)):
print("%d\n"%numbers[k])
n=[6,5,9,8,5,3]
print(sorted(n))

'''
posted on 2019-03-20 17:28  redOrange01  阅读(493)  评论(0编辑  收藏  举报