1. 程式人生 > >用 Python 排序資料的多種方法

用 Python 排序資料的多種方法

對python 中有元組 元組中有時間欄位,需要根據時間欄位來排序,

student_tuples = [
    ('john', 'A', 15),
    ('jane', 'B', 12),
    ('dave', 'B', 10),
]
>>> sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
 print(sorted(record_list,key=lambda student: student[1]))

可以採用這種方式