def make_gradebook():
return []
def make_module_info(module, grade):
module_info = (module,grade)
return module_info
def get_module(module_info):
return module_info[0]
def get_grade(module_info):
return module_info[1]
def get_level(module_info):
for i in module_info[0]:
if str.isdigit(i):
return i
def add_grade(gradebook, module_grade):
gradebook.append(module_grade)
return gradebook
def sort_by_module(gradebook):
gradebook.sort(key=lambda gradeb: gradeb[0])
return gradebook
def sort_by_level(gradebook):
gradebook.sort(key=lambda gradeb: get_level(gradeb))
return gradebook
def grade_to_numeric_marks(grade):
for i in grade:
if i[0] == 'A':
if i[1] == '+':
return 90
elif i[1] == '-':
return 80
else:
return 75
elif i[0] == 'B':
if i[1] == '+':
return 70
elif i[1] == '-':
return 65
else:
return 60
else:
if i[1] == '+':
return 55
elif i[1] == '-':
return 50
else:
return 46
return grade.sort()
def sort_by_grade(gradebook):
gradebook.sort(key=lambda gradeb: grade_to_numeric_marks(gradeb))
return gradebook
### for testing ###
gb = make_gradebook()
cs1010s = make_module_info('CS1010S', 'A+')
cs2020 = make_module_info('CS2020', 'A')
ma1101r = make_module_info('MA1101R', 'C')
ssa1207 = make_module_info('SSA1207', 'B+')
cs4247 = make_module_info('CS4247', 'A+')
ea5001 = make_module_info('EA5001', 'B')
add_grade(gb, cs1010s)
add_grade(gb, cs2020)
add_grade(gb, ma1101r)
add_grade(gb, ssa1207)
sort_by_grade(gradebook):
返回成绩单的新副本,按照模块等级从高到低排序。确保“A +”出现在'A'之前
我使用grade_to_numeric_marks(grade):
作为辅助函数,但我仍然无法得到我想要的输出[('CS1010S', 'A+'), ('CS2020', 'A'), ('SSA1207', 'B+'), ('MA1101R', 'C')]
出了什么问题?
答案 0 :(得分:2)
如果您不是进行多次if/elif/else
检查,而是定义字典等级>值和符号值,该怎么办?然后,在关键函数grade_to_numeric_marks
中,只需求每个等级的基点和每个符号的点数(+/-或空)。
例如(您可能需要调整每个等级/符号的值):
points = {'A': 80, 'B': 65}
signs = {'+': 10, '-': -5}
def grade_to_numeric_marks(item):
grade = item[1]
return points.get(grade[0], 50) + signs.get(grade[1:], 0)
def sort_by_grade(gradebook):
return sorted(gradebook, key=lambda x: grade_to_numeric_marks(x), reverse=True)
grades = [('CS1010S', 'A+'), ('MA1101R', 'C'), ('SSA1207', 'B+'), ('CS2020', 'A')]
print sort_by_grade(grades)
打印:
[('CS1010S', 'A+'), ('CS2020', 'A'), ('SSA1207', 'B+'), ('MA1101R', 'C')]
或者,正如@clutton在评论中提到的那样,只定义一个具有映射成绩的字典:
{'A+': 95, 'A': 90 ... }
然后,您可以简化排序:
points = {'A+': 95, 'A': 90, 'B+': 70, 'B': 65, 'C+': 55, 'C': 50} # need to define all of the possible grades
grades = [('CS1010S', 'A+'), ('MA1101R', 'C'), ('SSA1207', 'B+'), ('CS2020', 'A')]
print sorted(grades, key=lambda x: points.get(x[1]), reverse=True)
答案 1 :(得分:1)
这是一个OO重写:
class Module:
def __init__(self, name):
self.name = name
@property
def level(self):
for ch in self.name:
if ch.isdigit():
return int(ch)
class Grade:
grade_marks = {
"A+": 90,
"A-": 80,
"A": 75,
"B+": 70,
"B-": 65,
"B": 60,
"C+": 55,
"C-": 50,
"C": 46
}
def __init__(self, grade):
self.grade = grade
@property
def mark(self):
return Grade.grade_marks[self.grade]
class ModuleGrade:
def __init__(self, module, grade):
self.module = module if isinstance(module, Module) else Module(module)
self.grade = grade if isinstance(grade, Grade ) else Grade(grade)
def __str__(self):
return "{}: {}".format(self.module.name, self.grade.grade)
class StudentGrades:
sortkeys = {
"module": lambda mg: mg.module.name,
"level": lambda mg: mg.module.level,
"grade": lambda mg: -mg.grade.mark # Note: the -ve makes it descending order
}
def __init__(self, name, module_grades = None):
self.name = name
if module_grades is None:
self.module_grades = []
else:
self.module_grades = [mg if isinstance(mg, ModuleGrade) else ModuleGrade(*mg) for mg in module_grades]
def add_grade(self, *args):
if len(args) == 1:
mg = args[0]
if isinstance(mg, ModuleGrade):
self.module_grades.append(mg)
else:
self.module_grades.append(ModuleGrade(*mg))
elif len(args) == 2:
self.module_grades.append(ModuleGrade(*args))
else:
raise ValueError("Bad arguments to StudentGrades.add_grade")
def sort(self, key, reverse=False):
key = StudentGrades.sortkeys.get(key, key)
self.module_grades.sort(key=key, reverse=reverse)
def __str__(self):
return "\n".join([self.name] + [" {}".format(mg) for mg in self.module_grades])
以下是你如何使用它:
nf = StudentGrades("Norman Foster", [("CS1010S", "A+"), ("CS2020", "A")])
nf.add_grade("MA1101R", "C")
nf.add_grade("SSA1207", "B+")
nf.add_grade("CS4247", "A+")
nf.add_grade("EA5001", "B")
print(nf) # original insertion order
nf.sort("module")
print(nf) # alphabetical by module name
nf.sort("level", reverse=True)
print(nf) # in descending order by level
nf.sort("grade")
print(nf) # descending order by grade