250124 알고리즘(복습 완료)

송용진·2025년 1월 24일
0

알고리즘

목록 보기
168/173

31강 정렬

# 문자열 리스트를 길이에 따라 정렬
strs = ['aaaa', 'bb', 'ccc', 'd']
strs.sort(key=len)
print(strs)  # 출력: ['d', 'bb', 'ccc', 'aaaa']
# 중첩 리스트 정렬
nested_list = [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
nested_list.sort(key=lambda x: x[1])
print(nested_list)  # 출력: [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
# 중첩 리스트 내부 정렬
nested_list = [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
for sublist in nested_list:
    sublist.sort()
print(nested_list)  # 출력: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# 클래스 인스턴스 정렬
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

people = [Person('John', 20), Person('Jane', 19), Person('Adam', 21)]
people.sort(key=lambda x: x.age)
for person in people:
    print(person)
profile
백엔드 개발자

0개의 댓글