-1
我似乎無法弄清楚如何按學生ID對學生列表進行排序。我有一個字符串列表,每個字符串都包含學生的姓名和他們的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))
'sorted(students,key = lambda student:int(student.split(',')[1]))) ' –
https://docs.python.org/3/howto/sorting.html – wwii