2025年1月15日 星期三

sorted

 students = [('john', 'A', 20), ('mary', 'B', 30), ('jack','C', 60)]


students = sorted(students,key=lambda x:x[0])

print(students)

students = sorted(students,key=lambda x:x[1])

print(students)

students = sorted(students,key=lambda x:x[2])

print(students)

students = sorted(students,key=lambda x:x[2],reverse=True)

print(students)


# Output:


# [('jack', 'C', 60), ('john', 'A', 20), ('mary', 'B', 30)]

# [('john', 'A', 20), ('mary', 'B', 30), ('jack', 'C', 60)]

# [('john', 'A', 20), ('mary', 'B', 30), ('jack', 'C', 60)]

# [('jack', 'C', 60), ('mary', 'B', 30), ('john', 'A', 20)]


沒有留言:

張貼留言