我正在创建一个数据库程序,我正在尝试合并一个排序系统。我有多个对象,都是Students
。这些学生有三个属性:name
,grade
和average
。
我正在尝试让课程按照学生的平均分数进行排序,然后按照从最高到最低的平均顺序显示学生。我已经能够取消存储对象的外部文件,然后将每个学生的平均值存储到列表中。一旦我有了列表,我就可以使用MergeSort对列表进行排序。但是,我无法弄清楚如何使用排序列表来排序和显示每个对象。我曾尝试使用搜索来查找平均值在对象列表中的位置,因此我可以显示该对象,但我无法使其正常工作。
我的计划如下:
import pickle
class Student():
def __init__(self,nam,grd,avg):
self.name = nam
self.grade = grd
self.average = avg
def get_details(self):
print(self.name, self.grade, self.average)
def create_item():
new_student = Student(input("Enter name: "),input("Enter grade: "), input("Enter average: "))
save_object(new_student, 'student_data.pkl')
def clear_database():
file = open('student_data.pkl', 'w')
file.close()
def save_object(obj, filename):
with open(filename, 'ab') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def unpickle_database(filename):
with open(filename, 'rb') as f:
while True:
try:
yield pickle.load(f)
except EOFError:
break
def display_database():
print("\nName: Grade: Average: ")
for student in unpickle_database('student_data.pkl'):
property_list = [student.name, student.grade, student.average]
print(''.join([v.ljust(20,' ') for v in property_list]))
def delete_student(student_to_delete, filename):
students = list(unpickle_database(filename))
del students[student_to_delete-1]
clear_database()
for student in students:
save_object(student, filename)
def store_grades_in_list():
students = list(unpickle_database('student_data.pkl'))
students_grade = []
for student in students:
students_grade.append(int(student.grade))
return students_grade
def store_averages_in_list():
students = list(unpickle_database('student_data.pkl'))
students_average = []
for student in students:
students_average.append(int(student.average))
return students_average
def merge(st1, st2, st3):
i1 = i2 = i3 = 0
n1, n2 = len(st1), len(st2)
while i1 < n1 and i2 < n2:
if st1[i1] < st2[i2]:
st3[i3] = st1[i1]
i1 = i1 + 1
else:
st3[i3] = st2[i2]
i2 = i2 + 1
i3 = i3 + 1
while i1 < len(st1):
st3[i3] = st1[i1]
i1 = i1 + 1
i3 = i3 + 1
while i2 < len(st2):
st3[i3] = st2[i2]
i2 = i2 + 1
i3 = i3 + 1
def mergeSort(st):
n = len(st)
if n > 1:
m = n//2
st1, st2 = st[:m], st[m:]
mergeSort(st1)
mergeSort(st2)
merge(st1, st2, st)
return st
def bin_search(x):
my_list = store_averages_in_list()
bottom = 0
top = len(my_list)-1
found = False
location = -1
while (bottom <= top) and not(found):
middle = int((bottom + top)/2)
if (my_list[middle] == x):
location = middle
found = True
elif (my_list[middle] < x):
bottom = middle + 1
else:
top = middle -1
return location
while True:
user_input = input("\nType \"Clear\" to clear the database. Type \"Add\" to add a student. Type \"Display\" to display the database contents. Type \"Quit\" to quit the program. Type \"Remove\" to remove a student. Type \"Search\" to serach for an average. Type \"Sort av\" to display sorted list of averages.\n")
if user_input == "Quit":
break
elif user_input == "Clear":
clear_database()
print("\nThe database has been cleared.")
elif user_input == "Add":
Student.create_item()
print("\nThe student has been added. The updated database is: ")
display_database()
elif user_input == "Display":
display_database()
elif user_input == "Remove":
student_to_delete = int(input("Type the student number that you would like to delete: "))
delete_student(student_to_delete,'student_data.pkl')
print("\nThe student has been deleted. The updated database is: ")
display_database()
elif user_input == "Display Characteristics":
store_chars_in_list()
elif user_input == "Sort av":
print(mergeSort(store_averages_in_list()))
elif user_input == "Sort gr":
print(mergeSort(store_grades_in_list()))
elif user_input == "Search":
print(bin_search(int(input("Enter average to search for: "))))
答案 0 :(得分:2)
因此,您需要完成两件事。
Student
个对象的列表,而不是将学生 averages 的列表传递给mergesort函数。mergesort
功能不会发生变化,但merge
功能会发生变化。由于列表中的元素不再是数字,而是Student
个对象,而不是比较st1[i1] < st2[i2]
或类似对象,因此您需要比较st1[i1].average < st2[i2].average
之类的内容。这样,您就不会对平均值列表进行排序,而是对Student
列表进行排序。然后,一旦你有一个排序的学生列表,你可以随意显示它。这个答案故意尽可能少地使用明确的代码,因为这个问题感觉它被分配为学校作业,您将有机会获取我给出的信息并使用它: )
附注:如果您只想查找Student
的列表并且不需要或想要自己编写mergesort,则以下代码将起作用:
students = ... # list of Student
sorted_students = list(sorted(students, key=attrgetter('average'))
使用attrgetter
库中的operator
函数。