RUBY
del, pop() 딕셔너리 삭제(파이썬) 본문
del, pop() 딕셔너리 삭제(파이썬)
del과 key를 이용한 item 삭제
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
print(f'memInfo:{memInfo}')
del memInfo['메일']
print(f'memInfo:{memInfo}')
del memInfo['취미']
print(f'memInfo:{memInfo}')
memInfo:{'이름': '홍길동', '메일': 'gildong@gmail.com', '학년': 3, '취미': ['농구', '게임']}
memInfo:{'이름': '홍길동', '학년': 3, '취미': ['농구', '게임']}
memInfo:{'이름': '홍길동', '학년': 3}
memInfo = {'이름':'홍길동', '메일':'gildong@gmail.com', '학년':3, '취미':['농구','게임']}
print(f'memInfo:{memInfo}')
returnValue = memInfo.pop('이름')
print(f'memInfo: {memInfo}')
print(f'returnValue: {returnValue}')
print(f'returnValue type: {type(returnValue)}')
returnValue = memInfo.pop('취미')
print(f'memInfo: {memInfo}')
print(f'returnValue: {returnValue}')
print(f'returnValue type: {type(returnValue)}')
memInfo:{'이름': '홍길동', '메일': 'gildong@gmail.com', '학년': 3, '취미': ['농구', '게임']}
memInfo: {'메일': 'gildong@gmail.com', '학년': 3, '취미': ['농구', '게임']}
returnValue: 홍길동
returnValue type: <class 'str'>
memInfo: {'메일': 'gildong@gmail.com', '학년': 3}
returnValue: ['농구', '게임']
returnValue type: <class 'list'>
딕셔너리에 저장된 점수 중 최저 및 최고 점수를 삭제하는 프로그램을 만들어보자
scores = {'score1':8.9, 'score2':8.1, 'score3':8.5, 'score4':9.8, 'score5':8.8}
minScore = 10
minScoreKey = ''
maxScore = 0
maxScoreKey = ''
for key in scores.keys():
if scores[key] < minScore:
minScore = scores[key]
minScoreKey = key
if scores[key] > maxScore:
maxScore = scores[key]
maxScoreKey = key
del scores[minScoreKey]
del scores[maxScoreKey]
print(f'scores : {scores}')
scores : {'score1': 8.9, 'score3': 8.5, 'score5': 8.8}
'프로그래밍 언어 > Python' 카테고리의 다른 글
리스트를 이용한 프로그래밍(파이썬) (0) | 2023.01.24 |
---|---|
in, len(), clear() 딕셔너리(파이썬) (0) | 2023.01.23 |
keys()와 values() (파이썬) (0) | 2023.01.23 |
딕셔너리(Dictionary) 수정 (0) | 2023.01.23 |
딕셔너리(Dictionary) 추가 (0) | 2023.01.23 |
Comments