list.sort()
None
반환sorted(list)
reverse
list.sort(reverse=True)
key
정렬 목적으로 사용할 키를 반환하는 함수(or callable) 이어야 한다.
arr=[[1,2,3,4],[1],[2,6]]
arr.sort(key=len)
# output
# arr=[[1],[2,6],[1,2,3,4]]
student_tuples=[ ('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B' 10) ]
# sort by age
sorted(student_tuples, key=lambda student: student[2])
x[0]
를 기준으로 정렬하고 같을 경우 x[1]
를 기준으로 정렬하기arr=['abc','abe','act']
arr.sort(key=lambda x:(x[0],x[1]))
x[0]
를 기준으로 오름차순 정렬하고 같을 경우 x[1]
를 기준으로 내림차순 정렬하기arr=['abc','abe','act']
arr.sort(key=lambda x:(x[0],-x[1]))
References