데이터 구조 (Data Structure) 2
- 순서가 없는(unordered) 데이터 구조
- 세트(Set)
- 딕셔너리(Dictionary)
세트 (Set)
Mutable, unordered, iterable
추가 및 삭제
-
.add(ele)
:elem
을 세트에 추가합니다. -
.update(*others)
: 여러가지 값(iterable)을 추가합니다.a = {'starbucks'} a.update({'hollys', 'tomntoms'}, ['tomntoms', 'gongcha']) # {'starbucks', 'hollys', 'tomntoms', 'gongcha'} 중복제거, 순서는 랜덤
-
.remove(elem)
:elem
을 삭제하고, 없으면KeyError
가 발생합니다. -
.discard(elem)
:elem
을 삭제하고, 없어도 에러가 발생하지 않습니다. -
.pop()
: 임의의 원소를 제거하고 그 값을 반환합니다.
딕셔너리 (Dictionary)
Mutable, unordered, iterable
Key : Value
페어의 자료구조
조회
-
.get(key[, default])
:key
를 통해value
를 가져오며 절대KeyError
가 발생하지 않습니다.key
가 없을 경우 그 키에default
값을 넣어 새로운 페어를 생성합니다.default
는 기본적으로None
입니다.
my_dict = {'starbucks' : '카페', 'emart' : '마트', 'LGTWINS' : '프로야구구단'} print(my_dict['homeplus']) # KeyError 발생 print(my_dict.get('homeplus')) # None 반환 (기본값이 None) print(my_dict.get('homeplus', '마트')) # 마트 반환
추가 및 삭제
-
.pop(key[, default])
:key
가 딕셔너리에 있으면 제거 후 그 값을 반환합니다. 없을 경우default
값을 반환합니다.- 이 때,
default
값도 없을 경우KeyError
가 발생합니다.
- 이 때,
-
.update()
: 값을 제공하는key
,value
로 덮어씁니다.
my_dict = {'starbucks' : '카페', 'emart' : '마트'}
my_dict.update(starbucks = '도서관')
딕셔너리 순회 (반복문 활용)
-
dictionary에서
for
를 활용하는 4가지 방법key
활용.keys()
활용.values()
활용.items()
활용
for key in dict: print(dict[key]) for key in dict.keys(): print(dict[key]) for val in dict.values(): print(val) for key, val in dict.items(): print(key, val)
Dictionary comprehension
cubic = {x : x ** 3 for x in range(1, 5)}
print(cubic) # {1 : 1, 2 : 8, 3 : 27, 4 : 64}
dusts = {'서울': 72, '대전': 82, '구미': 29, '광주': 45}
result = {x : '나쁨' if y > 80 else '보통' for x, y in dusts.items()}
print(result) # {'서울': '보통', '대전': '나쁨', '구미': '보통', '광주': '보통'}
'TIL: Python' 카테고리의 다른 글
파이썬 데이터 구조 (Data Structure) 1: String, List (0) | 2020.08.09 |
---|---|
파이썬 에러와 예외 처리 (Error & Exception) (0) | 2020.08.08 |
파이썬 함수 (Function) 2: 스코프 (scope), 수명, 재귀 함수 (recursion) (0) | 2020.07.28 |
파이썬 함수(Function) 1: 선언, return, 인자 (0) | 2020.07.28 |
파이썬 제어문 (Control Flow) : if, for, while, break 등 (0) | 2020.07.26 |