1. 집합
- 집합의 기본 구조 : 중복되지 않는 데이터를 저장하는 자료형 가변적 {}/set() 순서가 없음 파이썬에서 가변적임
빈집합은 무조건 set()으로 만들어야 하고, 안에 채워진 내용이 있을 경우에만 {}를 사용
type은 dict
- 집합의 원소 추가 및 삭제 : add() 단일 원소 추가, update() 여러 원소 추가, remove() 특정 원소 제거 없으면 오류 발생, discard() 특정 원소 제거 없어도 오류 미발생
- 집합 연산 : 합집합 |, union() 교집합 &, intersection() 차집합 -, difference() 대칭차집합 ^, symmetric-difference()
- 집합 순회 : 집합은 순서가 없지만, 순회할 수 있음
- 집합 길이 확인 : len()
- 집합에서 in, not in 연산자 사용 가능
29731 | 7600 |
import sys input = sys.stdin.readline a = ['Never gonna give you up', 'Never gonna let you down', 'Never gonna run around and desert you', 'Never gonna make you cry', 'Never gonna say goodbye', 'Never gonna tell a lie and hurt you', 'Never gonna stop'] n = int(input()) count = 0 for i in range(n) : b = input().rstrip() if b in a : count += 1 if count == n : print("No") else : print("Yes") |
import sys import string input = sys.stdin.readline while True : a = input().rstrip() a = a.lower() if a == '#' : break l = [0] * 26 for x in a : if 0 <= ord(x)-97 <= 25 : l[ord(x)-97] += 1 count = 0 for x in l : if x > 0 : count += 1 print(count) |
3009 | |
import sys import string input = sys.stdin.readline a = [] b = [] for i in range(3) : x, y = map(int, input().split()) a.append(x) b.append(y) a.sort() b.sort() if a[0] == a[1] : aa = a[2] else : aa = a[0] if b[0] == b[1] : bb = b[2] else : bb = b[0] print(aa, bb) |
2. 딕셔너리
- 딕셔너리 기본 구조 : 키-값 쌍으로 데이터 저장, 순서 없으며 키는 고유해야 함, 시퀀스 객첸 아니지만 반복문을 통해 원소 순회 가능, 가변적, 중괄호 혹은 dict()으로 생성해 사용(둘다 초기 정의로 사용 가능)
- 딕셔너리의 원소 추가, 수정 및 삭제 : dict[key] = value로 추가, del 특정 키-값 쌍 삭제, pop() 제거 및 추출
- 딕셔너리의 키와 값 조회 : keys(), values() 키, 값 통째로 조회, items() 키-값 통째로 조회
- 딕셔너리 순회 : 딕셔너리의 키, 값, 키-값 쌍을 각각 반복문으로 순회할 수 있음
- 딕셔너리 길이 확인 : len()
- 딕셔너리 in, not in 연산자 사용 가능(키기준으로)
27889 | 2754 |
import sys import string input = sys.stdin.readline u = { 'NLCS' : 'North London Collegiate School', 'BHA' : 'Branksome Hall Asia', 'KIS' : 'Korea International School', 'SJA' : 'St. Johnsbury Academy'} a = input().rstrip() print(u[a]) |
import sys import string input = sys.stdin.readline u = { 'A+': 4.3, 'A0': 4.0, 'A-': 3.7, 'B+': 3.3, 'B0': 3.0, 'B-': 2.7, 'C+': 2.3, 'C0': 2.0, 'C-': 1.7, 'D+': 1.3, 'D0': 1.0, 'D-': 0.7, 'F': 0.0} a = input().rstrip() print(u[a]) |
3. 집합과 딕셔너리 활용
- 집합의 원소와 딕셔너리 키 제한 : 집합의 원소와 딕셔너리 키는 반드시 불변 객체여야 함, 리스트는 가변 객체이므로 집합의 원소나 딕셔너리 키로 사용 불가, 튜플은 불변 객체이므로 집합의 원소나 딕셔너리 키로 사용 가능
- 집합과 딕셔너리 컴프리헨션 : {x for x in range(10) if x %2 == 0}
- 리스트-집합, 리스트-딕셔너리 변환 : 리스트를 집합으로 변환하면 중복이 제거되며 다시 리스트로 변환하면 중복 없는 리스트를 얻을 수 있음, 리스트의 원소들은 딕셔너리의 키와 값으로 사용해 변환할 수 있음
a = {key : value for key, value in zip(keys, values)}
- collections.defaultdict 사용 : defaultdict는 키가 없는 경우, 기본값을 반환하도록 설정할 수 있는 딕셔너리
5597 | 31428 |
import sys import string input = sys.stdin.readline a = [0] * 31 for i in range(28) : n = int(input()) a[n] = 1 for i in range(1, 31) : if a[i] == 0 : print(i) |
import sys import string input = sys.stdin.readline n = int(input()) a = list(map(str, input().split())) b = input().rstrip() count = 0 for x in a : if x == b : count += 1 print(count) |
28691 | |
import sys import string input = sys.stdin.readline a = { 'M':'MatKor', 'W':'WiCys', 'C':'CyKor', 'A':'AlKor', '$':'$clear' } n = input().rstrip() print(a[n] |