Pokédex की तुलना
दो Pokémon ट्रेनर, Ash और Misty, अपनी-अपनी Pokémon कलेक्शन की तुलना करना चाहते हैं. आइए देखें कि उनके पास कौन-से Pokémon कॉमन हैं और कौन-से Pokémon Ash के पास हैं जो Misty के पास नहीं हैं.
Ash और Misty दोनों के Pokédex (उनकी Pokémon कलेक्शन) आपकी सेशन में ash_pokedex और misty_pokedex नाम की लिस्ट के रूप में लोड किए गए हैं. आपकी सुविधा के लिए इन्हें कंसोल में प्रिंट किया गया है.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Efficient Python Code लिखना
अभ्यास निर्देश
- दोनों लिस्ट (
ash_pokedexऔरmisty_pokedex) को क्रमशःash_setऔरmisty_setनाम के sets में बदलें. - एक set मेथड का उपयोग करके वे Pokémon ढूँढें जो Ash और Misty दोनों के पास कॉमन हैं.
- एक set मेथड से वे Pokémon ढूँढें जो Ash के Pokédex में हैं लेकिन नहीं हैं Misty के Pokédex में.
- एक set मेथड का उपयोग करके वे Pokémon ढूँढें जो या तो Ash या Misty में से किसी एक के लिए यूनिक हैं (अर्थात्, वे Pokémon जो सिर्फ एक Pokédex में मौजूद हैं, दोनों में नहीं).
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# 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)