比较图鉴(Pokédex)
两位宝可梦训练家 Ash 和 Misty 想要比较各自收集到的宝可梦。让我们看看他们有哪些是重合的,以及 Ash 拥有哪些是 Misty 没有的。
Ash 和 Misty 的 Pokédex(他们的宝可梦收藏)已作为列表 ash_pokedex 和 misty_pokedex 载入到您的会话中。为方便起见,它们也已打印到控制台。
本练习是课程的一部分
高效编写 Python 代码
练习说明
- 将两个列表(
ash_pokedex和misty_pokedex)分别转换为名为ash_set和misty_set的集合。 - 使用集合方法找出 Ash 和 Misty 共同拥有的宝可梦。
- 使用集合方法找出在 Ash 的 Pokédex 中但不在 Misty 的 Pokédex 中的宝可梦(即 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)