python基本统计数据
#获取用户不定长度的输入 def getNum(): nums = [] iNumStr = input("请输入数字(回车退出):") while iNumStr != "": nums.append(eval(iNumStr)) iNumStr = input("请输入数字(回车退出):") return nums #计算平均值 def mean(numbers): s = 0.0 for num in numbers: s += num return s/len(numbers) #计算方差 def dev(numbers,mean): sdev = 0.0 for num in numbers: sdev += (num - mean)**2 return pow(sdev/(len(numbers)-1),0.5) #计算中位数 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 n = getNum() m = mean(n) print("平均值:{},方差:{:.2},中位数:{}.".format(m,dev(n,m),median(n)))
作者:zhangshs
原文:https://my.oschina.net/zhangshsURL/blog/3008906
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16