Python 按学生id对学生列表进行排序

Python 按学生id对学生列表进行排序,python,python-3.x,Python,Python 3.x,我似乎不知道如何按学生ID对学生列表进行排序。我有一个字符串列表,每个字符串都包含学生的姓名和ID。看起来像这样: student_list = ["John,4", "Jake,1", "Alex,10"] ["Jake,1", "John,4", "Alex,10"] def sort_students_by_id(student_list): for string in student_list: comma = string.find(",")+1

我似乎不知道如何按学生ID对学生列表进行排序。我有一个字符串列表,每个字符串都包含学生的姓名和ID。看起来像这样:

student_list = ["John,4", "Jake,1", "Alex,10"]
["Jake,1", "John,4", "Alex,10"]
def sort_students_by_id(student_list):
    for string in student_list:
        comma = string.find(",")+1
        student = [(string[comma:])]    

        for index in range(len(student)):
            minpos = index
            for pos in range(index+1, len(student)):
                if student[pos] < student[minpos]:
                    minpos = pos
                tmp = student[index]
                student[index] = student[minpos]
                student[minpos] = tmp
            return student_list

print(sort_students_by_id(student_list))
我希望输出如下所示:

student_list = ["John,4", "Jake,1", "Alex,10"]
["Jake,1", "John,4", "Alex,10"]
def sort_students_by_id(student_list):
    for string in student_list:
        comma = string.find(",")+1
        student = [(string[comma:])]    

        for index in range(len(student)):
            minpos = index
            for pos in range(index+1, len(student)):
                if student[pos] < student[minpos]:
                    minpos = pos
                tmp = student[index]
                student[index] = student[minpos]
                student[minpos] = tmp
            return student_list

print(sort_students_by_id(student_list))
我的代码如下所示:

student_list = ["John,4", "Jake,1", "Alex,10"]
["Jake,1", "John,4", "Alex,10"]
def sort_students_by_id(student_list):
    for string in student_list:
        comma = string.find(",")+1
        student = [(string[comma:])]    

        for index in range(len(student)):
            minpos = index
            for pos in range(index+1, len(student)):
                if student[pos] < student[minpos]:
                    minpos = pos
                tmp = student[index]
                student[index] = student[minpos]
                student[minpos] = tmp
            return student_list

print(sort_students_by_id(student_list))
def按学生id排序学生(学生列表):
对于student_列表中的字符串:
逗号=字符串。查找(“,”)+1
学生=[(字符串[逗号:)]
对于范围内的索引(len(student)):
minpos=索引
对于范围内的pos(索引+1,len(学生)):
如果学生[pos]
您只需执行以下操作:

def sort_students_by_id(student_list):
    return sorted(student_list, key=lambda s: int(s.split(',')[-1]))

 # ['Jake,1', 'John,4', 'Alex,10']
 print(sort_students_by_id(student_list))
sorted(student,key=lambda student:int(student.split(',')[1]))