Python, 사전(Dictionary)
자료구조,
키와 값의 쌍으로 이루어짐
>>> d = dict(a=2,b=3,c=5)
>>> d
{'a': 2, 'c': 5, 'b': 3}
>>> type(d)
<class 'dict'>
>>> color = {'apple':'red','banana':'yellow'}
>>> color
{'apple': 'red', 'banana': 'yellow'}
>>> color['apple']
'red'
>>> color[0] #index는 지원하지 않음
-. 새로운 값 추가,변경
>>> color
{'apple': 'red', 'banana': 'yellow'}
>>> color['cherry'] = 'red'
>>> color
{'cherry': 'red', 'apple': 'red', 'banana': 'yellow'}
>>> color['apple'] = 'green'
>>> color
{'cherry': 'red', 'apple': 'green', 'banana': 'yellow'}
-. 사전의 내용 가져오기
>>> color.items()
dict_items([('cherry', 'red'), ('apple', 'green'), ('banana', 'yellow')])
>>> color.keys()
dict_keys(['cherry', 'apple', 'banana'])
>>> color.values()
dict_values(['red', 'green', 'yellow'])
-. 사전 삭제
>>> color
{'cherry': 'red', 'apple': 'green', 'banana': 'yellow'}
>>> del color['cherry']
>>> color
{'apple': 'green', 'banana': 'yellow'}
>>> color.clear()
>>> color
{}