Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Archives
Today
Total
관리 메뉴

RUBY

del, pop() 딕셔너리 삭제(파이썬) 본문

프로그래밍 언어/Python

del, pop() 딕셔너리 삭제(파이썬)

ruby-jieun 2023. 1. 23. 10:32

 

 

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}

 

 

 

Comments