• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

dictionary comprehension

24.04.23 12:19 작성 조회수 41

0

correct_score_dict = {name: score + 5 for (name, score) in incorrect_score_dict.items() if score < 80}

이 코드를 print 하면 {'Lisa' : 80} 만 출력되는데,

만약 80점 이하의 점수만 수정해서 원래대로

{'Tom': 80, 'Lisa':80, 'Sarah':90} 이렇게

출력 하려면 if 뒤에 추가적인 코드가 필요할 것 같은데 그런 경우에는 코드를 어떻게 작성해야할까요?

답변 1

답변을 작성해보세요.

0

안녕하세요 ithannag님,

질문을 잘 이해를 못하겠네요. 혹시 else를 넣고 싶으신건가요? 그렇다면 if를 앞으로 뺄 수도 있습니다.

incorrect_score_dict = {'Tom': 75, 'Lisa': 75, 'Sarah': 90}
correct_score_dict = {name: score + 5  if score < 80 else score for (name, score) in incorrect_score_dict.items()}
print(correct_score_dict)