RUBY
딕셔너리(Dictionary) 조회(파이썬) 본문
딕셔너리(Dictionary) 조회
키(key)와 값(value)를 이용해서 자료를 조회한다.
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':'박승철', 's5':'김지은'}
print('students[\'s1\']:{}'.format(students['s1']))
print('students[\'s2\']:{}'.format(students['s2']))
print('students[\'s3\']:{}'.format(students['s3']))
print('students[\'s4\']:{}'.format(students['s4']))
print('students[\'s5\']:{}'.format(students['s5']))
students['s1']:홍길동
students['s2']:박찬호
students['s3']:이용규
students['s4']:박승철
students['s5']:김지은
존재하지 않는 키를 이용한 조회 시 에러(error) 발생한다
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':'박승철', 's5':'김지은'}
print('students[\'s6\']:{}'.format(students['s6']))
print('students[\'s6\']:{}'.format(students['s6']))
KeyError: 's6'
get(key)를 이용해서 값(value)을 조회 할 수 있다.
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':'박승철', 's5':'김지은'}
print('students.get(\'s5\'):{}'.format(students.get('s5')))
students.get('s5'):김지은
get()은 key가 없어도 에러가 발생하지 않는다.
students = {'s1':'홍길동', 's2':'박찬호', 's3':'이용규', 's4':'박승철', 's5':'김지은'}
print('students.get(\'s6\'):{}'.format(students.get('s6')))
students.get('s6'):None
'프로그래밍 언어 > Python' 카테고리의 다른 글
딕셔너리(Dictionary) 수정 (0) | 2023.01.23 |
---|---|
딕셔너리(Dictionary) 추가 (0) | 2023.01.23 |
딕셔너리(Dictionary)(파이썬) (0) | 2023.01.23 |
튜플 while문을 이용한 조회(파이썬) (0) | 2023.01.23 |
튜플 for문을 이용한 조회(파이썬) (0) | 2023.01.23 |
Comments