set
전용 집합 메서드학생 때 배웠던
집합
연산은 파이썬에서도 가능하다.SET
타입은 수학의 집합을 표현하는 방식으로 중복을 허용하지 않으며 집합 연산을 쉽게 수행할 수 있도록 설계되어 있다.
교집합 (intersection
)
set1.intersection(set2)
set1 & set2
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result = set1.intersection(set2) # 결과: {2, 3}
합집합 (union
)
set1.union(set2)
set1 | set2
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2) # 결과: {1, 2, 3, 4, 5}
차집합 (difference
)
set1.difference(set2)
set1 - set2
set1 = {1, 2, 3}
set2 = {2, 3, 4}
result = set1.difference(set2) # 결과: {1}
대칭 차집합 (symmetric_difference
)
set1.symmetric_difference(set2)
set1 ^ set2
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.symmetric_difference(set2) # 결과: {1, 2, 4, 5}
def solution(lottos, win_nums):
rank = {6: 1, 5:2, 4:3, 3:4, 2:5, 1:6, 0:6}
count_zero = lottos.count(0)
matched_num = len(set(lottos) & set(win_nums))
max_rank = rank[matched_num + count_zero]
min_rank = rank[matched_num]
return [max_rank, min_rank]