按名称排序对象并按编号排序

时间:2019-07-13 05:19:48

标签: python-3.x

我需要有关按名称或编号对列表中的对象进行排序的帮助。

目前,我可以按值对它们进行排序,但我不能对名称进行相同的操作

ive尝试使用 def sort_names(t):     返回t [0]

def sort_courses_by_name(courses):     返回courses.sort(key = sort_names)

导入系统

            class Course:
                def __init__(self, name, units, term):
                    self.name = name
                    self.units = units
                    self.term = term

                def __repr__(self):
                    return f'<Course(name={self.name}, units={self.units}, term={self.term}>'

                def __str__(self):
                    return self.name + "        " + str(self.units) + "        " + self.term

                def __lt__(self, other):
                    # ALLOWS SORTING BY NAME
                    return self.units < other.units


            def show_menu():
                menu = "\nPlease choose 1 of the following options:\n\t1. List all courses\n\t2." \
                    " Add a course\n\t3. Drop a course\n\t4. Sort courses based on course name" \
                    "\n\t5. Sort courses based on number of units"
                print(menu)


            def list_courses(courses):
                if len(courses):
                    print('Your course list: ')
                    print('Name:' + "       " +'Units:' + "     " + 'Term: ')
                    for course in courses:
                        print(course)
                else:
                    print('\nYou are not currently taking any courses.')


            def add_course(courses):
                name = input('\nEnter course name: ')
                units = input('Enter the number of units for the course: ')
                term = input('Enter the term for this course: ')
                course = Course(name, units, term)
                courses.append(course)


            def drop_course(courses):

                if len(courses) != 0:
                    name = input('Enter the course name you wish to drop: ').strip()
                    found = False
                    for i in range(len(courses)):
                        if name == courses[i].name:
                            found = True
                            courses.pop(i)
                            break

                    if not found:
                        print("Sorry no class exists")
                else:
                    print("\nsorry, no classes to drop")


            def sort_courses_by_name(courses):
                return courses.sort(name)


            def sort_courses_by_units(courses):
                return courses.sort()


            def main():
                selection = 0
                courses = []
                while selection != 6:
                    show_menu()

                    try:
                        selection = int(input("\nEnter your option: "))
                    except ValueError:
                        print("\nSorry, Please pick a valid option")
                        continue

                    if selection == 1:
                        list_courses(courses)
                    elif selection == 2:
                        add_course(courses)
                    elif selection == 3:
                        drop_course(courses)
                    elif selection == 4:
                        sort_courses_by_name(courses)
                        print("~~~~~~~~~~~~")
                        list_courses(courses)
                    elif selection == 5:
                        sort_courses_by_units(courses)
                        list_courses(courses)
                    elif selection == 6:
                        print("good bye")
                        sys.exit
                    else:
                        print("Invalid entry, please enter an option from the menu")


            main()
课程:单位:学期: cist 4春季 拳头5夏天

输入一个选项:4(按名称排序)

课程:单位:学期: 拳头5夏天 cist 4春季

1 个答案:

答案 0 :(得分:0)

尝试按名称排序

def sort_courses_by_name(courses):
    return courses.sort(key = lambda x : x.name)