본문 바로가기
백준 - 파이썬/단계별 - 13 (정렬)

*[백준/파이썬] 11651번 좌표 정렬하기 2 | lambda함수 이용 정렬

by miiinn 2025. 10. 22.

 

[최종 코드]

import sys
n = int(sys.stdin.readline())

coor = []
for i in range(n):
    a, b = map(int, input().split())
    coor.append((a, b))

coor.sort(key=lambda x: (x[1], x[0]))

output_lines = [f'{x} {y}' for x, y in coor]
sys.stdout.write('\n'.join(map(str, output_lines)) + '\n')

[핵심 부분]

coor.sort(key=lambda x: (x[1], x[0]))

- lambda 함수를 이용해 (x, y)를 (y, x)로 바꾸기

output_lines = [f'{x} {y}' for x, y in coor]
sys.stdout.write('\n'.join(map(str, output_lines)) + '\n')

- 출력 형식 지정하여 조건에 맞게 출력하기