1) 구조
1. Key와 Value로 이루어진 파이썬의 특이한 구조
2. 표현: dic = {'Key1: 'Value1', 'Key2': 'Value2'}
ex)

dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}

2) dict에 추가, 삭제, 출력
1. 추가
2. 삭제: del 명령어 사용
3. 출력: dict['key']
* 위의 형태로 없는 key값을 가져오려고 하면 error가 발생한다. get 함수를 사용하면 error를 발생시키지 않고 'none'을 출력한다.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
print(dic)
# add key and value
dic['age'] = 30
print(dic)
# delete key and value
del dic['name']
print(dic)
# print value of specific key
print(dic['age'])
|
result
{'name': 'pey', 'phone': '0119993323', 'birth': '1118'}
{'name': 'pey', 'phone': '0119993323', 'birth': '1118', 'age': 30}
{'phone': '0119993323', 'birth': '1118', 'age': 30}
30
3) dict의 각종 함수
1. keys : dict의 key들을 객체로 반환. list로 응용하기위하여 list 함수로 감싸서 활용한다.
2. values: dict의 value를 list로 반환
3. clear: key value쌍 모두 지우기 (all clear)
4. get: key로 value 얻기
5. in: key가 dict에 있는지 조사(있으면 True, 없으면 false 반환)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
dic = {'name':'pey', 'phone':'0119993323', 'birth': '1118'}
dic_list = list( dic.keys() )
print(dic_list)
print(k)
print(dic_list)
value_list = dic.values()
print(value_list)
print(k)
print( 'name' in dic )
print( 'age' in dic )
|
result
['name', 'phone', 'birth']
name
phone
birth
['name', 'phone', 'birth']
dict_values(['pey', '0119993323', '1118'])
pey
0119993323
1118
pey
None
True
False
'Study > 파이썬' 카테고리의 다른 글
| 파이썬 예외처리방법 try except 구문사용법 (0) | 2019.12.31 |
|---|---|
| 파이썬 if __name__ == "__main__" (0) | 2019.12.31 |
| 파이썬 init과 self (0) | 2019.12.30 |
| 파이썬 리스트 인덱싱과 슬라이싱 (0) | 2019.12.29 |
| 파이썬 리스트 Vs 튜플 (0) | 2019.12.29 |