from math import sqrt
def mean(numbers):
s = 0.0
for num in numbers:
s = s+ num
return s / len(numbers)
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 dev(numbers,mean):
sdev = 0.0
for num in numbers:
sdev = sdev + (num - mean)**2
return sqrt(sdev / (len(numbers)-1))
def analyze(data):
list1 = []
list2 = []
dic = {}
for i in data:
list1.append(data[i]0[0,0])
list2.append(data[i]0[0,1])
dic['语文最高分'] = max(list1)
dic['数学最高分'] = max(list2)
dic['语文最低分'] = min(list1)
dic['数学最低分'] = min(list2)
dic['语文中位数'] = median(list1)
dic['数学中位数'] = median(list2)
dic['语文平均分'] = mean(list1)
dic['数学平均分'] = mean(list2)
dic['语文标准差'] = dev(list1,mean(list1))
dic['数学标准差'] = dev(list2,mean(list1))
return dic
data = {}
with open("C:\\Users\\Administrator\\Desktop\\成绩.txt", "r") as f:
lines = f.readlines()
for line in lines:
name, score1, score2, score3 = line.strip().split(", ")
data[name] = [int(score1), int(score2), int(score3)]
print(analyze(data))