RUBY
keys()와 values() (파이썬) 본문
keys()와 values()
전체 키(key)와 값(value)를 조회할 수 있다
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
ks = memInfo.keys()
print(f'ks : {ks}')
print(f'ks type : {type(ks)}')
ks : dict_keys(['이름', '메일', '학년', '취미'])
ks type : <class 'dict_keys'>
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
vs = memInfo.values()
print(f'vs : {vs}')
print(f'vs type : {type(vs)}')
vs : dict_values(['홍길동', 'gildong@gmail.com', 3, ['농구', '게임']])
vs type : <class 'dict_values'>
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
items = memInfo.items()
print(f'items : {items}')
print(f'items type : {type(items)}')
items : dict_items([('이름', '홍길동'), ('메일', 'gildong@gmail.com'), ('학년', 3), ('취미', ['농구', '게임'])])
items type : <class 'dict_items'>
리스트로 변환하기
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
ks = list(memInfo)
print(f'ks : {ks}')
print(f'ks type : {type(ks)}')
for문을 이용한 조회
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
for key in memInfo:
print(f'key: {key}')
for idx, key in enumerate(memInfo):
print(f'idx, key : {idx}, {key}')
key: 이름
key: 메일
key: 학년
key: 취미
idx, key : 0, 이름
idx, key : 1, 메일
idx, key : 2, 학년
idx, key : 3, 취미
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
for value in memInfo.values():
print(f'value: {value}')
for idx, value in enumerate(memInfo.values()):
print(f'idx, value : {idx}, {value}')
value: 홍길동
value: gildong@gmail.com
value: 3
value: ['농구', '게임']
idx, value : 0, 홍길동
idx, value : 1, gildong@gmail.com
idx, value : 2, 3
idx, value : 3, ['농구', '게임']
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
for item in memInfo.items():
print(f'item: {item}')
for idx, item in enumerate(memInfo.items()):
print(f'idx, item : {idx}, {item}')
item: ('이름', '홍길동')
item: ('메일', 'gildong@gmail.com')
item: ('학년', 3)
item: ('취미', ['농구', '게임'])
idx, item : 0, ('이름', '홍길동')
idx, item : 1, ('메일', 'gildong@gmail.com')
idx, item : 2, ('학년', 3)
idx, item : 3, ('취미', ['농구', '게임'])
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
for key in memInfo.keys():
print(f'key: {memInfo[key]}')
key: 홍길동
key: gildong@gmail.com
key: 3
key: ['농구', '게임']
학생의 시험 점수가 60점 미만이면 ‘F(재시험)’으로 값을 변경하는 코드를 keys()를 이용해서 작성해보자
scores = {'kor':88, 'eng':55, 'mat':85, 'sci':57, 'his':82}
print(f'scores: {scores}')
minScore = 60
fStr = 'F(재시험)'
fDic = {}
for key in scores:
if scores[key] < minScore:
scores[key] = fStr
fDic[key] = fStr
print(f'scores: {scores}')
print(f'fDic: {fDic}')
scores: {'kor': 88, 'eng': 55, 'mat': 85, 'sci': 57, 'his': 82}
scores: {'kor': 88, 'eng': 'F(재시험)', 'mat': 85, 'sci': 'F(재시험)', 'his': 82}
fDic: {'eng': 'F(재시험)', 'sci': 'F(재시험)'}
'프로그래밍 언어 > Python' 카테고리의 다른 글
in, len(), clear() 딕셔너리(파이썬) (0) | 2023.01.23 |
---|---|
del, pop() 딕셔너리 삭제(파이썬) (0) | 2023.01.23 |
딕셔너리(Dictionary) 수정 (0) | 2023.01.23 |
딕셔너리(Dictionary) 추가 (0) | 2023.01.23 |
딕셔너리(Dictionary) 조회(파이썬) (0) | 2023.01.23 |
Comments