分别用列表(list)和数组(ndarray)实现:
? ? ? ? (1)每位同学的总分,平均分;
? ? ? ? (2)每门课程的平均分,单科最高分,方差;
? ? ? ? (3)每位同学获得的绩点;
? ? ? ? (4)依据奖学金标准,判定该生能申请几等奖学金;
姓名 | 数分 | 高代 | 概率论 | 数理统计 | 张三 | 78 | 90 | 65 | 76 | 李四 | 89 | 84 | 90 | 82 | 王五 | 72 | 64 | 58 | 70 | 赵六 | 94 | 88 | 79 | 78 | 钱七 | 70 | 80 | 86 | 90 |
list = [['姓名','数分','高代','概率论','数理统计'],
['张三','78','90','65','76'],
['李四','89','84','90','82'],
['王五','72','64','58','70'],
['赵六','94','88','79','78'],
['钱七','74','80','86','90']]
listheight = len(list)#得到列表行数
listwidth = len(list[0])#得到列表列数
print("\n(1)、(3)、(4)")
sumc = [0 for r in range(listheight)]#造一个空列表存数据
for i in range(1,listheight):
credit = 0
minc = 100
for j in range(1,listwidth):
sumc[i] = sumc[i] + eval(list[i][j])#计算单人总成绩
if eval(list[i][j]) < minc :minc = eval(list[i][j])#求单人最低成绩
#计算绩点
if eval(list[i][j]) > 90: credit = credit + 4;continue
if eval(list[i][j]) > 85: credit = credit + 3.7;continue
if eval(list[i][j]) > 82: credit = credit + 3.3;continue
if eval(list[i][j]) > 78: credit = credit + 3.0;continue
if eval(list[i][j]) > 75: credit = credit + 2.7;continue
if eval(list[i][j]) > 72: credit = credit + 2.3;continue
if eval(list[i][j]) > 68: credit = credit + 2.0;continue
if eval(list[i][j]) > 64: credit = credit + 1.5;continue
if eval(list[i][j]) > 60: credit = credit + 1;continue
print("{}的总成绩是:{},平均分是:{:.4},绩点是:{:.2f}".format(list[i][0],sumc[i],sumc[i]/(listwidth-1),credit/(listwidth-1)),end="")
#判断申请的奖学金
if minc > 85:print(" 可以申请特等优秀学生奖学金\n")
elif minc > 80:print(",可以申请甲等优秀学生奖学金\n")
elif minc > 75:print(",可以申请乙等优秀学生奖学金\n")
elif minc > 70:print(",可以申请丙等优秀学生奖学金\n")
else:print("\n")
print("\n(2)")
for x in range(1,listwidth):
total = 0
maxc = 0
dnp = 0
for y in range(1,listheight):
if eval(list[y][x]) > maxc : maxc = eval(list[y][x]) #比较得出最高分
total = eval(list[y][x]) + total#计算单科累计分数
average = total / (listheight-1)#计算平均分
for z in range(1,listheight):#计算方差
dnp = dnp + (eval(list[z][x])-average)**2
Dnp = dnp/(listheight-1)
print("{}的平均分是{:.4},最高分是{},方差是{:.2f}\n".format(list[0][x],average,maxc,Dnp))
- 用数组实现
import numpy as np
data = [['姓名','数分','高代','概率论','数理统计'],
['张三','78','90','65','76'],
['李四','89','84','90','82'],
['王五','72','64','58','70'],
['赵六','94','88','79','78'],
['钱七','74','80','86','90']]
w1 = np.array(data)#创建数组
(a,b) = w1.shape#获得w1数组的维度
w2 = w1[1:a,1:b].astype(int)#提取数组data内的数据并转整形
w2add1 = np.sum(w2,axis = 1)#数组w2横轴的和:学生总成绩
w2average1 = np.mean(w2,axis = 1)#数组w2横轴的均值:学生平均分
w2average0 = np.mean(w2,axis = 0)#数组w2纵轴的均值:单科平均分
w2var0 = np.var(w2,axis = 0)#数组w2纵轴的方差:单科方差
w2max0 = w2.copy()
w2max0.sort(axis = 0)#w2数组纵向升序排序后的数组:单科最高分
w2max1 = w2.copy()
w2max1.sort(axis = 1)#w2数组横向升序排序后的数组:学生最低分
#计算绩点
w3 = np.array([])
for i in range(a-1):
credit = 0
for j in range(b-1):
if w2[i,j] > 90: credit = credit + 4;continue
if w2[i,j] > 85: credit = credit + 3.7;continue
if w2[i,j] > 82: credit = credit + 3.3;continue
if w2[i,j] > 78: credit = credit + 3.0;continue
if w2[i,j] > 75: credit = credit + 2.7;continue
if w2[i,j] > 72: credit = credit + 2.3;continue
if w2[i,j] > 68: credit = credit + 2.0;continue
if w2[i,j] > 64: credit = credit + 1.5;continue
if w2[i,j] > 60: credit = credit + 1;continue
w3 = np.append(w3,credit/(b-1))
print("(1)、(3)、(4)")
for x in range(0,a-1):
print("{}的总成绩是:{},平均分是:{:.4},绩点是:{:.2f}".format(w1[x+1,0],w2add1[x],w2average1[x],w3[x]),end="")
#判断申请的奖学金
if w2max1[x,0] > 85:print(" 可以申请特等优秀学生奖学金\n")
elif w2max1[x,0] > 80:print(",可以申请甲等优秀学生奖学金\n")
elif w2max1[x,0] > 75:print(",可以申请乙等优秀学生奖学金\n")
elif w2max1[x,0] > 70:print(",可以申请丙等优秀学生奖学金\n")
else:print("\n")
print("(2)")
for y in range(0,b-1):#计算方差
print("{}的平均分是{:.4},最高分是{},方差是{:.2f}\n".format(w1[0,y+1],w2average0[y],w2max0[a-2,y],w2var0[y]))
以上是我的一点学习笔记。
|