list1=["國文","數學","自然"]
list2=[["a","國文",100],
["a","數學",20],
["a","自然",40],
["b","國文",30],
["b","數學",100],
["c","自然",60]
]
# 科目的總數
total_number_of_subjects = len(list1)
# 科目 <-> 科目流水號的對應
subject_idx = { subject:list1.index(subject) for subject in list1 }
# 學生 <-> 各科成績的對應
student_score = {}
# 讀取資料,填入學生和各科成績到student_score
for student, subject, score in list2:
if student not in student_score:
student_score[student] = [ None for _ in range(total_number_of_subjects) ]
student_score[student][ subject_idx[subject] ] = score
result = []
# 輸出每個學生的成績資料到result
for student in sorted(student_score.keys()):
cur_entry = [student] + student_score[student]
result.append(cur_entry)
print(result)