- 키(key)와 값(value)값을 이용해서 자료 관리
- 키(key) : 작성자가 지정할 수 있는 인덱스 (절대 중복되면 안됨)
- 딕셔너리 선언
'{ }' 사용, '키:값'으로 하나의 아이템 정의- key 와 value에는 '문자(열), 숫자, 논리형, 컨테이너 자료형'도 올 수 있다.
- menInfo={'나이' : 20, '취미' : [농구,축구]}
- ★ key에는 불변의 내용만(uniq함), 가변 내용은 올 수 없음 ★
- '리스트' 컨테이너 자료 X ('튜플'은 가능)
딕셔너리명[key명] #해당 'key'의 'value'값을 추출
Ex - dicName['eng'] = 'abc' 일 경우,
dicName['eng'] ='가나다'
print(dicName['eng'])
-> '가나다'
없는 key값 추출시 에러가 뜸
get ( ) 함수 이용 : 없는 값이면 'none'이라고 뜸
딕셔너리명.get(key)
딕셔너리명 = {} #임의로 선언 먼저 해놓기
딕셔너리명[key명] = value #해당 딕셔너리에 {키1:벨류1,키2:벨류2 ...}으로 추가
myInfo = { }
myInfo['이름'] = '홍길동'
myInfo['나이'] = 20
myInfo['주소'] = '서울'
myInfo['취미'] = ['농구','축구']
print(myInfo)
myInfo['연락처'] = input('연락처 : ')
#-------------출력시-------------
{'이름' : '홍길동', '나이':20, '주소': '서울','취미':['농구','축구']}
연락처 :
#-------------수정 방법----------
myInfo['나이'] = 30 #딕셔너리는 중복 안되므로, 20에서 30으로 value값 변경됨
menInfo={'이름' : '홍길동', '나이':20, '주소':'서울','취미':['농구','축구']}
menKey=menInfo.keys()
menValue=menInfo.values()
menItem=menInfo.items()
print(menKey)
print(menValue)
print(menItem) # 튜플 형식으로 아이템 추출
#-------------출력시-------------
dict_keys(['이름','나이','주소','취미'])
dict_values(['홍길동',20,'서울',['농구','축구']])
dict_items([('이름','홍길동'),('나이',20),('주소','서울'),('취미', ['농구','축구'])])
#----------리스트(list) 변환----------
menKey=list(menKey)
menValue=list(menValue)
menItem=list(menItem)
print(menKey)
print(menValue)
print(menItem)
#-------------출력시-------------
['이름','나이','주소','취미']
['홍길동',20,'서울',['농구','축구']]
[('이름','홍길동'),('나이',20),('주소','서울'),('취미', ['농구','축구'])]
del 딕셔너리명[key명] # 'key : value' 통으로 아이템 삭제
딕셔너리명.clear ( )
scores ={'kor':88, 'eng':55,'mat':85,'sci':57,'his':82}
scores.clear()
print(scores)
#-------------출력시-------------
{}
len(딕셔너리명)
menInfo={'이름' : '홍길동', '나이':20, '주소':'서울','취미':['농구','축구']}
print('이름' in memInfo)
print('학교' not in memInfo)
print('학교' in memInfo)
#-------------출력시-------------
True
True
False