【问题标题】:how would i print all the names and scores of those users from a particular class?我将如何打印特定班级中这些用户的所有姓名和分数?
【发布时间】:2016-03-01 14:05:27
【问题描述】:

让我换个问题。我将如何从所需的班级中提取所有用户信息(分数、班级编号和分数)。例如,我想知道让班级编号 1 中的所有用户信息。

帮助,schooldata[x]['class_code'] = 用户输入的班级编号 schooldata[x]['score'] = 该学生获得的分数 schooldata[x]['name'] = 该用户的姓名 [x] 是学生,所以第一个用户是 [0],第二个用户是 [1] 等等......

    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']))

【问题讨论】:

  • 不清楚你在问什么,或者代码 sn-p 与你的“问题”有什么关系
  • 那么..有什么问题?您的预期与实际输出是多少?你给你的程序什么输入?你有错误吗?不要要求我们为你做功课。我们可以帮助您解决困难,但不要要求我们完成整个任务。
  • 你想要schooldata[0]schooldata[1]schooldata[2]的平均分?
  • 例如,如果我是老师,我想知道一个班级所有分数的平均分。因此,如果 3 个用户输入他们在 1 号班级,那么第 2 个用户将是 2 号班级,并说第 3 个用户输入他们在 1 号班级。我作为老师想要找到所有用户的平均分数让我们说第 1 课。所以当老师输入是时,我的代码应该输出第 1 课中所有用户的姓名和该课的平均分数。希望这是有道理的
  • 这是一段巨大的代码。您能否提及您用于存储值的数据结构示例以及您想要哪个值的平均值?

标签: python sorting average


【解决方案1】:

根据更新的问题更新答案:

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

打印类数据(有序):

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']

【讨论】:

  • 我希望这是一个玩笑的答案。
  • @TimPietzcker 有什么更好的方法吗?我不确定 OP 想知道什么,但这个答案是基于我对问题的理解。但我想知道实现这一目标的更好方法(我认为你有)。
  • 您自己将其编辑为您的答案。除非你有充分的理由,否则不要使用reduce()。这不是一个。
  • schooldata[x]['class_code'] 是用户存储班级编号的地方。
  • 让我换个问题。我将如何从所需的班级中提取所有用户信息(分数、班级编号和分数)。例如,我想知道让班级编号 1 中的所有用户信息。
猜你喜欢
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2021-11-08
  • 2018-10-29
  • 2013-06-22
相关资源
最近更新 更多