如何打印所有DS成绩在50分以上的学生姓名?



我有一些名单和字典,上面有学生的名字和分数。

如何打印所有DS得分超过50分的学生人数及其姓名?

students = ['Sara', 'Zineb', 'Mohamed', 'Ali', 'Khadija',
'Idriss', 'Najat', 'Nadia', 'Marouane', 'Ahmed']
scores = {'DS': [[36, 58, 46, 96, 9, 82, 83, 66, 35, 47],
[46, 50, 55, 21, 22, 76, 51, 90, 96, 48],
[56, 54, 53, 17, 31, 74, 11, 53, 98, 67],
[77, 38, 8, 74, 39, 39, 52, 66, 38, 86],
[93, 21, 7, 33, 10, 97, 48, 96, 24, 7],
[97, 98, 95, 75, 64, 9, 48, 51, 45, 82]],
'TP': [[48, 63, 98, 47, 25, 90, 100, 21, 41, 44],
[73, 79, 78, 39, 11, 100, 57, 96, 13, 99]]}

共有10名学生,每个DS列表中有10个整数。

如果我们想要一组得分超过50分的学生:我们可以(1)创建一个所有学生的集合,(2)遍历列表,(3)从集合中丢弃得分低于50分的学生。(4)集合中的剩余值告诉我们有一个学生('Nadia')的总分超过50分:

more_than_50 = set(students)
for score_list in scores['DS']:
for student, score in zip(students, score_list):
if score < 50:
more_than_50.discard(student)
print(len(more_than_50), more_than_50)
1 {'Nadia'}

你可以尝试用这个简单的方法:

for i in len(students):
count=0 
for row in scores['DS']:
if row[i]>50:
count+=1 
if count==len(scores):#if all the grades > 50
print(students [i])

最新更新