포케덱스 비교하기
두 포켓몬 트레이너 Ash와 Misty가 각자 모은 포켓몬 컬렉션을 비교하려고 해요. 두 사람이 공통으로 보유한 포켓몬과, Misty에게는 없지만 Ash에게만 있는 포켓몬을 확인해 봅시다.
Ash와 Misty의 포케덱스(두 사람이 모은 포켓몬 목록)는 각각 ash_pokedex, misty_pokedex라는 리스트로 세션에 로드되어 있어요. 편의를 위해 콘솔에 출력해 두었습니다.
이 연습은 강의의 일부입니다
효율적인 Python 코드 작성
연습 안내
- 두 리스트(
ash_pokedex,misty_pokedex)를 각각ash_set,misty_set이라는 세트로 변환하세요. - 세트 메서드를 사용해 Ash와 Misty가 공통으로 가진 포켓몬을 찾으세요.
- 세트 메서드를 사용해 Ash의 포케덱스에는 있지만 Misty의 포케덱스에는 없는 포켓몬을 찾으세요.
- 세트 메서드를 사용해 Ash 또는 Misty 중 한 사람에게만 있는, 즉 두 포케덱스 중 정확히 하나에만 존재하는 고유 포켓몬을 찾으세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Convert both lists to sets
ash_set = ____
misty_set = ____
# Find the Pokémon that exist in both sets
both = ____.____(____)
print(both)
# Find the Pokémon that Ash has and Misty does not have
ash_only = ____.____(____)
print(ash_only)
# Find the Pokémon that are in only one set (not both)
unique_to_set = ____.____(____)
print(unique_to_set)