Python 如何打印特定类中这些用户的所有姓名和分数?
让我换个问题。如何从所需的类中提取所有用户信息(分数、类号和分数)。例如,我想知道第1类中的所有用户信息 要提供帮助,请输入schooldata[x]['class_code']=用户键入的班级编号schooldata[x]['score']=该学生获得的分数 学校数据[x]['name']=该用户的姓名 [x] 是学生,因此第一个用户将是[0],第二个用户[1]等Python 如何打印特定类中这些用户的所有姓名和分数?,python,sorting,average,Python,Sorting,Average,让我换个问题。如何从所需的类中提取所有用户信息(分数、类号和分数)。例如,我想知道第1类中的所有用户信息 要提供帮助,请输入schooldata[x]['class_code']=用户键入的班级编号schooldata[x]['score']=该学生获得的分数 学校数据[x]['name']=该用户的姓名 [x] 是学生,因此第一个用户将是[0],第二个用户[1]等 schooldata = [] for x in range (0,3): #the number of
schooldata = []
for x in range (0,3): #the number of loops the quiz is run which is 3 times
score = 0
quiz = dict()
print ("Enter your name")
quiz['name'] = input()
print ("what class")
quiz['class_code'] = input()
print("1. 9+10=")
answer = input()
answer = int(answer)
if answer == 19:
print("correct")
score = score + 1
else:
print("wrong")
print("2. 16+40=")
answer = input()
answer = int(answer)
if answer == 56:
print("correct")
score = score + 1
else:
print("wrong")
print("3. 5+21=")
answer = input()
answer = int(answer)
if answer == 26:
print("correct")
score = score + 1
else:
print("wrong")
print("4. 5-6=")
answer = input()
answer = int(answer)
if answer == -1:
print("correct")
score = score + 1
else:
print("wrong")
print("5. 21-9=")
answer = input()
answer = int(answer)
if answer == 12:
print("correct")
score = score + 1
else:
print("wrong")
print("6. 12-11=")
answer = input()
answer = int(answer)
if answer == 1:
print("correct")
score = score + 1
else:
print("wrong")
print("7. 5*6=")
answer = input()
answer = int(answer)
if answer == 30:
print("correct")
score = score + 1
else:
print("wrong")
print("8. 1*8=")
answer = input()
answer = int(answer)
if answer == 8:
print("correct")
score = score + 1
else:
print("wrong")
print("9. 4*6=")
answer = input()
answer = int(answer)
if answer == 24:
print("correct")
score = score + 1
else:
print("wrong")
print("10. 9*10=")
answer = input()
answer = int(answer)
if answer == 90:
print("correct")
score = score + 1
else:
print("wrong")
quiz['score'] = score
schooldata.append(quiz)
print ("name - ", schooldata[0]['name'],", user score - ", schooldata[0]['score'],", class number - ", schooldata[0]['class_code'])
print ("name - ", schooldata[1]['name'],", user score - ", schooldata[1]['score'],", class number - ", schooldata[1]['class_code'])
print ("name - ", schooldata[2]['name'],", user score - ", schooldata[2]['score'],", class number - ", schooldata[2]['class_code'])
#high to low
sorted_schooldata = sorted(schooldata, key=lambda k: k['score'])[::-1]
#alphabetical
for i in sorted(schooldata, key=lambda k: k['name']):
print('%s:%s' %(i['name'], i['score']))
根据更新的问题更新答案:
classdata = {}
for data in schooldata:
if classdata.get(data['class_code']):
classdata[data['class_code']].append(data)
else:
classdata[data['class_code']] = [data]
print classdata
要打印类数据(按顺序),请执行以下操作:
不清楚您在问什么,或者代码片段与您的“问题”有什么关系,所以。。有什么问题吗?您的预期和实际产出是多少?你给你的程序什么输入?你有错误吗?不要让我们帮你做家庭作业。我们可以帮助您解决问题,但不要要求我们完成整个作业。您需要的是
schooldata[0]
,schooldata[1]
,schooldata[2]
?例如,如果我是老师,我想知道一个班级所有成绩的平均分。因此,如果3个用户输入他们在1类中,那么第2个用户将是2类,并说第3个用户类型是1类。作为老师,我想找出1班所有用户的平均分数。因此,当老师输入yes时,我的代码应该输出1号班所有用户的姓名和该班的平均分数。希望有意义这是一段巨大的代码。你能说说你用来存储值的数据结构的样本吗?你想平均哪个值?我希望这是一个笑话。@TimPietzcker有更好的方法吗?我不确定OP想知道什么,但这个答案是基于我对问题的理解。但我想知道实现这一目标的更好方法(我认为你已经做到了),你自己把它编辑成了你的答案。除非你有充分的理由,否则不要使用reduce()
。这不是一个。schooldata[x]['class_code']是用户存储其班级编号的地方。让我更改问题。如何从所需的类中提取所有用户信息(分数、类号和分数)。例如,我想知道第1类中的所有用户信息。
for class_data in sorted(classdata):
for person_data in sorted(classdata[class_data], key=lambda x: x['name']):
print person_data['class_code'], person_data['name'], person_data['score']