객체를 정렬하는 방법에 대해 배우게 됩니다.
lambda
는 이름 없이 사용하는 익명함수f = lambda x: x * 2 # lambda 인자 : 반환값
print(f(3)) # 6
class Student:
def __init__(self, kor, eng, math):
self.kor = kor
self.eng = eng
self.math = math
students = [
Student(90, 80, 80),
...
Student(30, 40, 10),
]
students.sort(key=lambda x: x.kor) # 국어 점수 기준 오름차순 정렬
for student in students:
print(student.kor, student.eng, student.math)
students.sort(key=lambda x: -x.kor) # 국어 점수 기준 내림차순 정렬
students = [
(90, 80, 90),
...
(30, 40, 70),
]
students.sort(key=lambda x: x[0]) # 국어 점수 기준 오름차순 정렬
for kor, eng, math in students:
print(kor, eng, math)
class Student:
def __init__(self, name, height, weight):
self.n = name
self.h = height
self.w = weight
n = int(input())
students = []
for _ in range(n):
name, height, weight = input().split()
students.append(Student(name, int(height), int(weight)))
students.sort(key = lambda x: x.h)
for student in students:
print(student.n, student.h, student.w)
n = int(input())
# 이름, 키, 몸무게
ls = [tuple(input().split()) for _ in range(n)]
ls.sort(key = lambda x: x[1])
for name, height, weight in ls:
print(name, height, weight)
여러 우선순위를 갖는 경우 lambda 함수의 반환값을 단일 값이 아닌, tuple값으로 정의
여러 개 (예: 2개) 의 원소로 이루어진 tuple끼리 비교 연산을 하는 경우
1.먼저 첫 번째 원소를 기준으로 비교
2.첫 번째 원소가 동일하다면 두 번째 원소를 기준으로 비교
위의 tuple 특성을 이용해 lambda 함수의 반환값에 우선순위대로 각 기준이 되는 값을 적어준다.
# 첫 번째 우선순위는 국어 점수 오름차순
# 국어 점수가 같다면 두 번째 우선순위는 영어 점수 오름차순
students.sort(key=lambda x: (x.kor, x.eng))
# 국어 점수 오름차순, 영어 점수 내림차순의 경우
students.sort(key=lambda x: (x.kor, -x.eng))
# 첫 번째 우선순위 : 국어 점수 오름차순, 두 번째 우선순위 : 영어 점수 내림차순
students.sort(key=lambda x: (x[0], -x[1]))
class Student:
def __init__(self, name, kor, eng, math):
self.name, self.kor, self.eng, self.math = name, kor, eng, math
n = int(input())
students = []
for _ in range(n):
name, k, e, m = input().split()
students.append(Student(name, int(k), int(e), int(m)))
# 점수가 높은 학생부터 출력하므로 내림차순 정렬
students.sort(key = lambda x : (-x.kor, -x.eng, -x.math))
for student in students:
print(student.name, student.kor, student.eng, student.math)
n = int(input())
students = []
for _ in range(n):
name, kor, eng, math = input().split()
students.append((name, int(kor), int(eng), int(math)))
# 점수가 높은 학생부터 출력하므로 내림차순 정렬
# tuple 0: 이름, 1: 국어, 2: 영어, 3: 수학
students.sort(key = lambda x : (-x[1], -x[2], -x[3]))
for name, kor, eng, math in students:
print(name, kor, eng, math)
## Class ##
students.sort(key=lambda x: x.kor + x.eng + x.math)
## Tuple ##
students.sort(key=lambda x: x[0] + x[1] + x[2])
예) 국어 점수 기준인데, 국어 점수가 30의 배수인 경우 먼저 나오도록 하는 정렬
## custom comparator 정의 ##
# x가 앞에 있는 학생, y가 뒤에 있는 학생이라 가정
# 이 순서가 원하는 순서면 0보다 작은 값 (-1),
# 반대라면 0보다 큰 값 (1),
# 둘의 우선순위가 동일하다면 0 반환
def compare(x, y):
# x만 국어 점수가 30의 배수라면 x가 더 앞에 있어야 함
# 현재 순서가 True
if x.kor % 30 == 0 and y.kor % 30 != 0:
return -1
# y만 국어 점수가 30의 배수라면 y가 더 앞에 있어야 함
# 현재 순서가 False
if x.kor % 30 != 0 and y.kor % 30 == 0:
return 1
# 우선 순위가 동일한 경우
# 즉, 국어 점수가 둘 다 30의 배수이거나 둘 다 30의 배수가 아닌 경우
return 0
students.sort(key = cmp_to_key(compare))
class Student:
def __init__(self, name, s1, s2, s3):
self.name, self.s1, self.s2, self.s3 = name, s1, s2, s3
n = int(input())
students = []
for _ in range(n):
name, s1, s2, s3 = input().split()
students.append(Student(name, int(s1), int(s2), int(s3)))
students.sort(key = lambda x : x.s1 + x.s2 + x.s3)
for student in students:
print(student.name, student.s1, student.s2, student.s3)
n = int(input())
students = []
for _ in range(n):
name, s1, s2, s3 = input().split()
students.append((name, int(s1), int(s2), int(s3)))
students.sort(key = lambda x : x[1] + x[2] + x[3])
for name, s1, s2, s3 in students:
print(name, s1, s2, s3)
# 정렬 이후 등수별 학생 번호 출력, 1등부터 시작 (start = 1)
for idx, student in enumerate(students, start=1):
print(f'{idx}등: {student.number}번')
>> 1등: 1번
2등: 3번
3등: 5번
4등: 4번
5등: 2번
for idx, (_, _, _, number) in enumerate(students, start=1):
print(f'{idx}등: {student.number}번')
num_to_rank = [0] * 6 # 5명이므로 1번부터 시작
nums = [1, 3, 5, 4, 2] # 학생 번호
# (등수, 번호)
# (1, 1), (2, 3), (3, 5), (4, 4), (5, 2)
for rank, num in enumerate(nums, start=1):
num_to_rank[num] = rank
print(num_to_rank) # [0, 1, 5, 2, 4, 3]
class Student:
def __init__(self, height, weight, number):
self.height, self.weight, self.number = height, weight, number
n = int(input())
students = []
for i in range(1, n + 1):
h, w = map(int, input().split())
students.append(Student(h, w, i))
students.sort(key = lambda x : (-x.height, -x.weight, x.number))
for student in students:
print(student.height, student.weight, student.number)
n = int(input())
students = []
for i in range(1, n + 1):
h, w = map(int, input().split())
students.append((h, w, i))
students.sort(key = lambda x : (-x[0], -x[1], x[2]))
for h, w, num in students:
print(h, w, num)