Python 사전(dict)

2020/Python 2019. 11. 19. 12:56
반응형

#dict 예제
def define_dict():
""" 사전의 정의 """
# 기본적인 사전의 생성
dct = dict() # 빈 사전
print(dct, type(dct))
# Literal 이용한 생성 {}
dct = {"basketball": 5, "baseball": 9}
#키에 접근하고자 할 경우
print(dct["baseball"]) #baseball 키에 연결된 값을 참조
#없는 키에 연결된 값의 참조
#print(dct["soccer"]) # -> KeyError
dct["soccer"] = 11 #새 값을 넣을 경우 새로운 키가 생성됩니다.
print("dct:", dct)

# 순서가 없기 때문에 인덱싱, 슬라이싱 불가합니다.
# len, 포함 여부(in,not in) 확인 가능합니다.
# -> 기본적으로 대상이 Key를 기준으로 한다(값이 아니다)

# 길이의 확인 :len
print(dct, "LENGTH:", len(dct))

# in, not in 키를 검색할 수 있다
print("soccer" in dct) # 키 목록에서 soccer 검색
print("volleyball" in dct) # 키 목록에서 volleyball 검색.. False 나와요

#dict는 복합 자료형입니다. 키의 목록, 값의 목록을 별도로 뽑아낼 수 있습니다.
print("KEYS OF dct:", dct.keys()) #keys 메서드 -> 키목록이 출력됩니다.
print("Type of keys:", type(dct.keys())) #타입이 출력됩니다.
print("Values of dct:", dct.values()) #values 메서드 -> 값목록이 출력됩니다.

# 값의 포함 여부를 판단하려면 .values() dict_values를 추출
# 그 안에서 확인
# dct의 값에 9가 포함되어 있는가?
print("dct의 값에 9가 포함되어 있습니까?", 9 in dct.values()) # 출력: true

#사전을 생성하는 다른 방법들은 무엇이 있을까요?
# 키워드 인자를 이용한 사전의 생성
d1 = dict(key1="value1", key2="value2", key3="value3")
print("d1:", d1, type(d1))

#튜플의 리스트로 사전의 생성
d2 = dict([("key1", 1), ("key2", 2), ("key3", 3)])
print("d2:", d2, type(d2))

# 키의 목록과 값이 이미 목록에 있는 경우
# zip 객체로 묶어서 dict에 전달
keys = ("one", "two", "three", "four")
values = (1,2,3,4)
d3 = dict(zip(keys, values))
print("d3:", d3, type(d3))

#사전의 키는 immutable 자료형이어야 합니다.
d4 = {}
d4[True] = "true"
d4[10] = 10
d4["eleven"] = 11
d4[("홍길동", 23)] = "홍길동 23"

# bool, 수치형, 문자열, 튜플 등 불변 자료형 가능
# d4[["홍길동", 23]] = "홍길동 23" # -> Error
print("d4:", d4)

def dict_methods():
""" 사전의 메서드들"""
dct = {"soccer":11, "baseball": 9, "volleyball": 6}
print("dct:", dct)
#key의 목록 추출 : keys 메서드들
keys = dct.keys()
print("keys of dict:", keys, type(keys))
#dict_keys는 순차 자료형으로 변환할 수 있다.
keys_list = list(keys)
print(keys_list)
#값의 목록 추출: values 메서드
values = dct.values()
print("Values of dct:", values, type(values))
# -값 쌍튜플의 목록 추출
pairs = dct.items()
print("key-vale pair of dct:", pairs)

dct.clear() # 비우기
print("dct:", dct)

def using_dict():
""" 사전 사용 연습"""

phones = {
"홍길동": "010-0000-0000",
"장길산": "010-1111-1111",
"임꺽정": "010-2222-2222"
}
print(phones)

#새로운 키의 추가 ['']
phones['일지매'] = "010-3333-3333"

#키 직접 접근 vs get 비교
if "고길동" in phones:
print(phones['고길동']) # 키 직접 접근은 키가 없으면 에러 발생
print(phones.get("고길동")) # get 메서드는 키가 없을 경우 None 리턴
#키가 없어도 기본값을 리턴하고자 할 경우 get메서드 두번째 인자로
#기본 값을 부여
print(phones.get("고길동", "미등록"))

#삭제 : del
if "일지매" in phones:
del phones['일지매']
print(phones)

#pop 메서드 : 값을 가져오며 해당 객체를 삭제
print(phones.pop('장길산'))
print(phones)
#popitem 메서드 : -벨류 쌍튜플을 반환하고 키를 삭제
item = phones.popitem()
print("Name:", item[0], "Phone:", item[1])
print(phones)

def loop():
""" 사전 객체의 순회 """
phones = {
"홍길동": "010-0000-0000",
"장길산": "010-1111-1111",
"임꺽정": "010-2222-2222"
}
print(phones)

# 기본적인 loop
for key in phones: # 루프를 진행하면 keys() 목록을 대상으로 한다
print(key, ":" , phones[key])

# 키와 값을 함께 loop
for t in phones.items(): # item 메서드는 키-값 쌍튜플의 목록
print(t)

for key, value in phones.items():
print(key, ":", value)



if __name__ =="__main__":
define_dict()
dict_methods()
using_dict()
loop()

반응형
블로그 이미지

꽃꽂이하는개발자

,