Python字典应用:学生成绩列表输出

题目内容

假设有一个存放学生学号和语数英三门功课成绩的列表studs如下:
studs= [{‘sid’:‘103’, ‘Chinese’: 90, ‘Math’:95, ‘English’:92},{‘sid’:‘101’, ‘Chinese’: 80, ‘Math’:85, ‘English’:82}, {‘sid’:‘102’, ‘Chinese’: 70, ‘Math’:75, ‘English’:72}]。编写程序,将列表studs的数据内容提取出来,放到一个字典scores中,在屏幕上按学号从小到大的顺序显示输出所有学生的学号及语数英三门功课的成绩。

求解代码

# 原始数据构成的列表,列表中每一个元素是一个字典
studs= [{'sid':'103', 'Chinese': 90, 'Math':95, 'English':92},\
        {'sid':'101', 'Chinese': 80, 'Math':85, 'English':82},\
        {'sid':'102', 'Chinese': 70, 'Math':75, 'English':72}]
studs.sort(key=lambda x:eval(x['sid']))
# 创建一个名为scores的字典并记录原始数据
scores=dict()
for student in studs:
          scores[student['sid']]=list()
          scores[student['sid']].append(student['Chinese'])
          scores[student['sid']].append(student['Math'])
          scores[student['sid']].append(student['English'])
# 输出字典结果
for score in scores:
          print('{}:{}'.format(score,scores[score]))

输出结果

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2023年11月9日
下一篇 2023年11月9日

相关推荐