7.对一个存储学生成绩的字典进行操作。能往字典写入数据能从字典中提取出各项数据,并进行比如求和、求平均、排序、统计数量等运算。
现有一个字典存放着学生的学号和成绩。成绩列表里的3个数据分别是学生的语文、数学和英语成绩:
dict={'01':[67,88,45],'02':[97,68,85],'03':[97,98,95],'04':[67,48,45],'05':[82,58,75],'06':[96,49,65]}
完成以下操作:
1)编写函数,返回每门成绩均大于等于85的学生的学号。
def main(dict):
for i in list(dict.keys()):
scores = dict[i]
if scores[0]>=85 and scores[1]>=85 and scores[2]>=85:
print(i)
dict={'01':[67,88,45],'02':[97,68,85],'03':[97,98,95],'04':[67,48,45],'05':[82,58,75],'06':[96,49,65]}
main(dict)
2)编写函数,返回每一个学号对应的平均分(sum和len)和总分(sum),结果保留两位小数。'''
def main(dict):
for i in list(dict.keys()):
print(i, round(sum(dict[i]) / len(dict[i]), 2), round(sum(dict[i]), 2))
dict={'01':[67,88,45],'02':[97,68,85],'03':[97,98,95],'04':[67,48,45],'05':[82,58,75],'06':[96,49,65]}
main(dict)
3.编写函数,返回按总分升序排列的学号列表。
def main(dict):
result = sorted(dict.items(), key=lambda x: sum(x[1]), reverse=True)
print([i[0] for i in result])
dict={'01':[67,88,45],'02':[97,68,85],'03':[97,98,95],
'04':[67,48,45],'05':[82,58,75],'06':[96,49,65]}
main(dict)
是要设计函数还是怎样呢?
xuesheng = dict()
while True:
if len(xuesheng):
print('do you want write or read?')
text = input('请输入 w 或者 r:')
if text == 'r':
print("学生总成绩:{0}, 平均成绩:{1},学生各科成绩排名:{2}, 学生学习课目数:{3}".format(sum(xuesheng.values()),
sum(xuesheng.values()) / len(xuesheng),
sorted(xuesheng.values()),
len(xuesheng.keys())))
elif text == 'w':
xuesheng[input('请输入考试课目:')] = eval((input('请输入考试分数:')))
print(xuesheng)
else:
print('请重新输入w或者r')
continue
else:
print('请增加一个学生成绩信息')
xuesheng[input('请输入考试课目:')] = eval(input('请输入考试分数:'))