상세 컨텐츠

본문 제목

컬렉션 타입(세트, 딕셔너리)

인공지능_2026/1. 파이썬

by Ryuzy 2026. 4. 16. 12:41

본문

반응형

1. 세트

세트(set) 중복되지 않는 항목들의 순서가 없는 컬렉션입니다. 중괄호 {} 사용하여 생성하거나 set() 생성자를 사용할  있습니다.

 

1. 세트 만들기

s1 = {1, 3, 5, 7}
print(s1)
print(type(s1))

s2 = {}
print(s2)
print(type(s2))

s3 = set([1, 3, 5, 7])
print(s3)
print(type(s3))

li4 = [1, 2, 3, 4]
print(type(li4))
s4 = set(li4)
print(s4)
print(type(s4))

s5 = {1, 3, 5, 3, 7, 9, 1, 5, 10, 7}
print(s5)

li6 = [1, 3, 5, 3, 7, 9, 1, 5, 10, 7]
print(li6)
s6 = set(li6)
print(s6)

 

2. 세트의 메소드

s1 = {1, 3, 5, 7}
print(s1)

# add(): 세트의 요소를 추가
s1.add(2)
print(s1)
s1.add(4)
print(s1)
s1.add(7)
print(s1)

 

s1 = {1, 3, 5, 7}
print(s1)

# update(): 세트에 여러 요소를 추가
s1.update([2, 4, 6, 8, 10])
print(s1)

 

s1 = {1, 3, 5, 7}
print(s1)

# remove(): 세트의 요소를 제거. 단 요소가 없으면 에러가 발생
s1.remove(3)
print(s1)
# s1.remove(3) # KeyError: 3

 

s1 = {1, 3, 5, 7}
print(s1)

# discard(): 세트의 요소를 제거. 단 요소가 없어도 에러가 발생하지 않음
s1.discard(3)
print(s1)
s1.discard(3)
print(s1)

 

s1 = {1, 3, 5, 7}
print(s1)

s2 = s1 # 메모리 주소를 복사
print(id(s1))
print(id(s2))

# copy(): 세트를 복사
s2 = s1.copy() # 값을 복사
print(s1)
print(s2)
print(id(s1))
print(id(s2))

 

s3 = {10, 20, 30, 40, 50}
s4 = {30, 40, 50, 60, 70}

# union(): 합집합을 계산하여 반환
result1 = s3.union(s4)
print(result1)

result2 = s3 | s4
print(result2)

 

s3 = {10, 20, 30, 40, 50}
s4 = {30, 40, 50, 60, 70}

# intersection(): 교집합을 계산하여 반환
result1 = s3.intersection(s4)
print(result1)

result2 = s3 & s4
print(result2)

 

s3 = {10, 20, 30, 40, 50}
s4 = {30, 40, 50, 60, 70}

# difference(): 차집합을 계산하여 반환
result1 = s3.difference(s4)
print(result1)

result2 = s3 - s4
print(result2)

 

s3 = {10, 20, 30, 40, 50}
s4 = {30, 40, 50, 60, 70}

# symmetric_difference(): 대칭 차집합을 계산하여 반환
result1 = s3.symmetric_difference(s4)
print(result1)

result2 = s3 ^ s4
print(result2)

 

 

2. 딕셔너리

파이썬의 딕셔너리는 - 쌍을 저장하는 순서 없는 변경 가능한(mutable) 컬렉션입니다. (파이썬 3.7 이전까지는 순서 없음 (3.7 이후는 입력 순서를 유지하지만 "논리적으로는" 순서 없음으로 간주) 딕셔너리는 중괄호 {} 사용하여 생성하고, 키-값 쌍들은 쉼표 , 구분됩니다.  키-값 쌍은 콜론 :으로 구분됩니다.

 

1. 딕셔너리 만들기

dic1 = {}
print(dic1)
print(type(dic1))

dic2 = {1:'김사과', 2:'반하나', 3:'오렌지', 4:'이메론'}
print(dic2)
print(type(dic2))

print(dic2[1])
print(dic2[3])

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)
print(dic3['userid'])
print(dic3['hp'])

 

2. 딕셔너리 수정하기

딕셔너리에 - 쌍을 추가하거나 제거하거나, 기존의 키의 값을 변경할  있습니다. 단, 딕셔너리의 키는 변경 불가능한(immutable) 타입이어야 합니다. 예를 들어, 문자열, 정수, 튜플은 딕셔너리의 키로 사용할  있지만, 리스트는 딕셔너리의 키로 사용할  없습니다. 하지만 딕셔너리의 값은 어떤 타입이든 상관없습니다.

dic4 = {1:'apple'}
print(dic4)

dic4[100] = 'banana'
print(dic4)

dic4[100] = 'orange' # 변경
print(dic4)

dic4[50] = 'melon'
print(dic4)

del dic4[100] # 삭제
print(dic4)

 

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# 요소 추가
dic3['gender'] = 'female'
print(dic3)

# 요소의 값 변경
dic3['no'] = 10
print(dic3)

dic3['score'] = [100, 90, 40]
print(dic3)

# dic3[[10, 20, 30]] = ['십', '이십', '삼십']
# print(dic3)

dic3[(10, 20, 30)] = ['십', '이십', '삼십']
print(dic3)

dic3['과일'] = {'사과':'🍎', '딸기':'🍓', '수박':'🍉'}
print(dic3)

 

3. 함수와 메서드

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# keys(): 딕셔너리의 모든 키를 반환
print(dic3.keys())

 

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# values(): 딕셔너리의 모든 값을 반환
print(dic3.values())

 

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# items(): 딕셔너리의 모든 키-값을 튜플로 반환
print(dic3.items())

 

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# get(): 특정 키에 대한 값을 반환. 만약 키가 딕셔너리에 없으면 None을 반환
# None을 치환할 수 있는 문자열을 설정할 수 있음
print(dic3['userid'])
# print(dic3['gender']) # KeyError: 'gender'
print(dic3.get('userid'))
print(dic3.get('gender')) # None
print(dic3.get('gender', '성별 알수없음'))
print(dic3.get('name', '이름 알수없음'))

 

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# pop(): 특정 키에 대한 값을 제거하고 제거된 값을 반환. 키가 없다면 에러
temp = dic3.pop('hp')
print(dic3)
print(temp)

 

dic3 = {'no':1, 'userid':'apple', 'name':'김사과', 'hp':'010-1111-1111'}
print(dic3)

# in: 딕셔너리에 특정 키가 있는지 확인
print('hp' in dic3)
print('010-1111-1111' in dic3)
 
반응형

'인공지능_2026 > 1. 파이썬' 카테고리의 다른 글

제어문 - 반복문  (0) 2026.04.17
제어문 - 조건문  (0) 2026.04.17
컬렉션 타입(리스트, 튜플)  (1) 2026.04.15
연산자  (0) 2026.04.14
파이썬의 입력과 출력  (0) 2026.04.14

관련글 더보기