객체를 정렬하는 방법에 대해 배우게 됩니다.
class Student:
def __init__(self, name, height, weight):
self.name, self.height, self.weight = name, height, weight
students = []
for _ in range(5):
name, height, weight = input().split()
students.append(Student(name, int(height), float(weight)))
# 이름순으로 정렬, 오름차순
name_sort = sorted(students, key = lambda x: x.name)
# 키가 큰 순으로 정렬, 내림차순
height_sort = sorted(students, key = lambda x: -x.height)
print('name')
for student in name_sort:
print(student.name, student.height, student.weight)
print()
print('height')
for student in height_sort:
print(student.name, student.height, student.weight)
class Student:
def __init__(self, name, height, weight):
self.name, self.height, self.weight = name, height, weight
students = []
for _ in range(5):
name, height, weight = input().split()
students.append(Student(name, int(height), float(weight)))
# 이름순으로 정렬, 오름차순
students.sort(key = lambda x: x.name)
print('name')
for student in students:
print(student.name, student.height, student.weight)
print()
# 키가 큰 순으로 정렬, 내림차순
students.sort(key = lambda x: -x.height)
print('height')
for student in students:
print(student.name, student.height, student.weight)
students = []
for _ in range(5):
name, height, weight = input().split()
students.append((name, int(height), float(weight)))
# 이름순으로 정렬, 오름차순
students.sort(key = lambda x: x[0])
print('name')
for n, h, w in students:
print(n, h, w)
print()
# 키가 큰 순으로 정렬, 내림차순
students.sort(key = lambda x: -x[1])
print('height')
for n, h, w in students:
print(n, h, w)
class Student:
def __init__(self, name, height, weight):
self.name, self.height, self.weight = name, height, 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.height, -x.weight))
for student in students:
print(student.name, student.height, student.weight)
n = int(input())
students = []
for _ in range(n):
name, height, weight = input().split()
students.append((name, int(height), int(weight)))
# 키 오름차순, 키가 동일한 경우 몸무게 내림차순
students.sort(key = lambda x: (x[1], -x[2]))
for name, height, weight in students:
print(name, height, weight)
# 점의 개수
n = int(input())
spots = []
for i in range(1, n + 1):
x, y = map(int, input().split())
spots.append((x, y, i))
# 원점과 (x, y) 사이가 가까운 순으로 정렬
spots.sort(key = lambda x: abs(x[0]) + abs(x[1]))
for _, _, num in spots:
print(num)
|x1 - x2| + |y1 - y2|
이므로 원점과의 거리는 |x| + |y|
n = int(input())
inputs = list(map(int, input().split()))
# 동일한 원소인 경우 먼저 나오는 원소가 더 앞으로
elems = []
for idx, num in enumerate(inputs, start=1):
elems.append((num, idx)) # 편의를 위해 (원소, 위치)
# 오름차순 정렬, 동일한 원소일 경우 idx 오름차순
elems.sort(key = lambda x: (x[0], x[1]))
# 정답 리스트
result = [0] * (n + 1)
for idx, elem in enumerate(elems, start=1):
result[elem[1]] = idx # elem == (원소, 위치)
print(*result[1:])
for idx, (_, i) in enumerate(elems, start=1):
result[i] = idx
class Student:
def __init__(self, height, weight, number):
self.h, self.w, self.num = height, weight, number
n = int(input())
students = []
for num in range(1, n + 1):
h, w = map(int, input().split())
students.append(Student(h, w, num))
# 1순위: 키 오름차순, 2순위: 몸무게 내림차순
students.sort(key = lambda x: (x.h, -x.w))
for student in students:
print(student.h, student.w, student.num)
n = int(input())
students = []
for num in range(1, n + 1):
h, w = map(int, input().split())
students.append((h, w, num))
# 1순위: 키 오름차순, 2순위: 몸무게 내림차순
students.sort(key = lambda x: (x[0], -x[1]))
for h, w, num in students:
print(h, w, num)