我正在编写以下代码来计算学生的综合成绩以管理他们的成绩单,但没有获得所需的输出,有人可以帮助我吗,Thanx!
def transcript(coursedetails, studentdetails, grades):
studentdetails.sort()
coursedetails.sort()
grades.sort()
result=[(student,grade,coursedetail)
for student in studentdetails
for grade in grades for coursedetail in coursedetails
if((student[0]==grade[0])and(grade[1]==coursedetail[0]))]
print(result)
我给的输入是---
transcript([("MA101","Calculus"),("PH101","Mechanics"),("HU101","English")],[("UGM2018001","Rohit Grewal"),("UGP2018132","Neha Talwar")],[("UGM2018001","MA101","AB"),("UGP2018132","PH101","B"),("UGM2018001","PH101","B")])
实际输出---
[(('UGM2018001', 'Rohit Grewal'), ('UGM2018001', 'MA101', 'AB'), ('MA101', 'Calculus')), (('UGM2018001', 'Rohit Grewal'), ('UGM2018001', 'PH101', 'B'), ('PH101', 'Mechanics')), (('UGP2018132', 'Neha Talwar'), ('UGP2018132', 'PH101', 'B'), ('PH101', 'Mechanics'))]
期望输出---
[('UGM2018001', 'Rohit Grewal', [('MA101', 'Calculus', 'AB'), ('PH101', 'Mechanics', 'B')]), ('UGP2018132', 'Neha Talwar', [('PH101', 'Mechanics', 'B')])]
答案 0 :(得分:0)
您可以考虑将结构从元组列表更改为字典,因为操作信息会更容易。
def transcript(coursedetails, studentdetails, grades):
result = {}
students = {}
courses = {}
for course, descript in coursedetails:
courses[course] = descript
for sid, student in studentdetails:
students[sid] = {'name':student}
for sid, course, grade in grades:
details = (course, courses[course], grade)
if not 'grades' in students[sid]:
students[sid]['grades'] = []
students[sid]['grades'].append(details)
return students
这将返回您的成绩单词典字典,其中学生ID是返回包含学生姓名和成绩的字典的键。
测试:
trans = transcript([("MA101","Calculus"),("PH101","Mechanics"),("HU101","English")],[("UGM2018001","Rohit Grewal"),("UGP2018132","Neha Talwar")],[("UGM2018001","MA101","AB"),("UGP2018132","PH101","B"),("UGM2018001","PH101","B")])
结果:
{'UGM2018001': {'name': 'Rohit Grewal', 'grades': [('MA101', 'Calculus', 'AB'), ('PH101', 'Mechanics', 'B')]}, 'UGP2018132': {'name': 'Neha Talwar', 'grades': [('PH101', 'Mechanics', 'B')]}}
如果您仍希望在上述所需输出中获取此信息,则以下内容将完全按照您的需要创建元组列表。
list((k,v['name'], v['grades']) for k,v in trans.items())
答案 1 :(得分:0)
这可以通过collections.defaultdict
:
from collections import defaultdict
inputs = [("MA101","Calculus"),("PH101","Mechanics"),("HU101","English")],\
[("UGM2018001","Rohit Grewal"),("UGP2018132","Neha Talwar")],\
[("UGM2018001","MA101","AB"),("UGP2018132","PH101","B"),("UGM2018001","PH101","B")]
def transcript(coursedetails, studentdetails, grades):
d = defaultdict(list)
courses, students = dict(coursedetails), dict(studentdetails)
for grade in grades:
d[(grade[0], students[grade[0]])].append((grade[1], courses[grade[1]], grade[2]))
return d
res = transcript(*inputs)
<强>结果强>
defaultdict(list,
{('UGM2018001', 'Rohit Grewal'): [('MA101', 'Calculus', 'AB'),
('PH101', 'Mechanics', 'B')],
('UGP2018132', 'Neha Talwar'): [('PH101', 'Mechanics', 'B')]})
如果确实需要一个元组列表,这很容易转换:
res2 = [(k[0], k[1], v) for k, v in res.items()]
# [('UGM2018001', 'Rohit Grewal', [('MA101', 'Calculus', 'AB'),
# ('PH101', 'Mechanics', 'B')]),
# ('UGP2018132', 'Neha Talwar', [('PH101', 'Mechanics', 'B')])]