[Python] sort() method

Gi Woon Lee·2024년 8월 25일
0

TIL

목록 보기
46/78

L.sort()메서드

list.sort(reverse=True|False, key=myFunc)

  • reverse: reverse=False(ascending)_default , optional
  • key: A FUNCTION to sepecify the sorting criteria(s), optional, 함수를 만들어서 기준을 정해줘야한다.
  • 예시
def myFunc(e):
  return len(e)

cars = ['Ford', 'Mitsubishi', 'BMW', 'VW']

cars.sort(reverse=True, key=myFunc)

print(cars)
# ['Mitsubishi', 'Ford', 'BMW', 'VW']

0개의 댓글