简体   繁体   中英

How can I get a random selection from one dictionary that is a subset of another, without hitting recursion limit so often?

Disclaimer: I have checked many questions with similar keywords, but I don't believe they address my situation. Please correct me if I'm wrong, but I can't find anything.

I'm trying to get coverage from a random choice from the Pokedex where the values in the Pokedex dict will correspond to enough keys in the Effective_Against dict, which in turn will result in a subset of the set Types.

So basically, I want to randomly create a team where you will always have a type advantage against any opposing Pokemon. My code is in all sorts of functions and different files, so for the sake of simplicity, I've included what I believe will be enough to explain my problem, but I'll also walk through an example.

EXAMPLE: If Bulbasaur was randomly chosen for the team from the Pokedex, then my team's types would include grass and poison. Grass is effective against ground, rock, and water, so my team would be effective against those 3 types from the set Types. I want a random choice of Pokemon that will be effective against all of the types in the set Types.

Problem is, I keep hitting maximum recursion depth before it gets a result. I need a way to make this more efficient somehow.

Types = {"normal", "flying", "fighting", "poison", "ground", "rock", "bug", "ghost", "fire", "water", "grass", "electric", "psychic", "ice", "dragon"}

Effective_Against = {
    "normal": [],
    "fighting": ["normal", "rock", "ice"],
    "flying": ["fighting", "bug", "grass"],
    "poison": ["bug", "grass"],
    "ground": ["poison", "rock", "fire", "electric"],
    "rock": ["flying", "bug", "fire", "ice"],
    "bug": ["poison", "grass", "psychic"],
    "ghost": ["ghost"],
    "fire": ["bug", "grass", "ice"],
    "water": ["ground", "rock", "fire"],
    "grass": ["ground", "rock", "water"],
    "electric": ["flying", "water"],
    "psychic": ["fighting", "poison"],
    "ice": ["flying", "ground", "grass", "dragon"],
    "dragon": ["dragon"],
    "steel": [],
    "dark" : [],
    "fairy": [],
}

pokedex = {
'bulbasaur': ['grass', 'poison'], 
'ivysaur': ['grass', 'poison'], 
'venusaur': ['grass', 'poison'],
'charmander': ['fire']
} # and so on, so forth

while len(team) < 6:
   pokemon = random.choice(list(pokedex))
   team.append(pokemon)

types_effective_against = # the types that my Pokemon have coverage against 

if Types.issubset(types_effective_against):
        return True

I suggest iterating on the types that you have to cover, starting by types that are hard to cover (for instance, normal is harder to cover than grass, because only fighting is strong against normal, whereas fire, ice, poison and bug are all strong against grass).

Then, select a random pokemon which covers the next type.

If you already have 6 pokemon and some types are still uncovered, then erase the whole team and try again.

import random # random.choice, random.sample

pokedex = dict([('Bulbasaur', ['grass', 'poison']), ('Ivysaur', ['grass', 'poison']), ('Venusaur', ['grass', 'poison']), ('Charmander', ['fire']), ('Charmeleon', ['fire']), ('Charizard', ['fire', 'flying']), ('Squirtle', ['water']), ('Wartortle', ['water']), ('Blastoise', ['water']), ('Caterpie', ['bug']), ('Metapod', ['bug']), ('Butterfree', ['bug', 'flying']), ('Weedle', ['bug', 'poison']), ('Kakuna', ['bug', 'poison']), ('Beedrill', ['bug', 'poison']), ('Pidgey', ['normal', 'flying']), ('Pidgeotto', ['normal', 'flying']), ('Pidgeot', ['normal', 'flying']), ('Rattata', ['normal', 'dark']), ('Raticate', ['normal', 'dark']), ('Spearow', ['normal', 'flying']), ('Fearow', ['normal', 'flying']), ('Ekans', ['poison']), ('Arbok', ['poison']), ('Pikachu', ['electric']), ('Raichu', ['electric', 'electric']), ('Sandshrew', ['ground', 'ice']), ('Sandslash', ['ground', 'ice']), ('Nidoran♀', ['poison']), ('Nidorina', ['poison']), ('Nidoqueen', ['poison', 'ground']), ('Nidoran♂', ['poison']), ('Nidorino', ['poison']), ('Nidoking', ['poison', 'ground']), ('Clefairy', ['fairy']), ('Clefable', ['fairy']), ('Vulpix', ['fire', 'ice']), ('Ninetales', ['fire', 'ice']), ('Jigglypuff', ['normal', 'fairy']), ('Wigglytuff', ['normal', 'fairy']), ('Zubat', ['poison', 'flying']), ('Golbat', ['poison', 'flying']), ('Oddish', ['grass', 'poison']), ('Gloom', ['grass', 'poison']), ('Vileplume', ['grass', 'poison']), ('Paras', ['bug', 'grass']), ('Parasect', ['bug', 'grass']), ('Venonat', ['bug', 'poison']), ('Venomoth', ['bug', 'poison']), ('Diglett', ['ground', 'ground']), ('Dugtrio', ['ground', 'ground']), ('Meowth', ['normal', 'dark']), ('Persian', ['normal', 'dark']), ('Psyduck', ['water']), ('Golduck', ['water']), ('Mankey', ['fighting']), ('Primeape', ['fighting']), ('Growlithe', ['fire']), ('Arcanine', ['fire']), ('Poliwag', ['water']), ('Poliwhirl', ['water']), ('Poliwrath', ['water', 'fighting']), ('Abra', ['psychic']), ('Kadabra', ['psychic']), ('Alakazam', ['psychic']), ('Machop', ['fighting']), ('Machoke', ['fighting']), ('Machamp', ['fighting']), ('Bellsprout', ['grass', 'poison']), ('Weepinbell', ['grass', 'poison']), ('Victreebel', ['grass', 'poison']), ('Tentacool', ['water', 'poison']), ('Tentacruel', ['water', 'poison']), ('Geodude', ['rock', 'ground']), ('Graveler', ['rock', 'ground']), ('Golem', ['rock', 'ground']), ('Ponyta', ['fire']), ('Rapidash', ['fire']), ('Slowpoke', ['water', 'psychic']), ('Slowbro', ['water', 'psychic']), ('Magnemite', ['electric', 'steel']), ('Magneton', ['electric', 'steel']), ("Farfetch'd", ['normal', 'flying']), ('Doduo', ['normal', 'flying']), ('Dodrio', ['normal', 'flying']), ('Seel', ['water']), ('Dewgong', ['water', 'ice']), ('Grimer', ['poison', 'poison']), ('Muk', ['poison', 'poison']), ('Shellder', ['water']), ('Cloyster', ['water', 'ice']), ('Gastly', ['ghost', 'poison']), ('Haunter', ['ghost', 'poison']), ('Gengar', ['ghost', 'poison']), ('Onix', ['rock', 'ground']), ('Drowzee', ['psychic']), ('Hypno', ['psychic']), ('Krabby', ['water']), ('Kingler', ['water']), ('Voltorb', ['electric']), ('Electrode', ['electric']), ('Exeggcute', ['grass', 'psychic']), ('Exeggutor', ['grass', 'psychic']), ('Cubone', ['ground']), ('Marowak', ['ground', 'fire']), ('Hitmonlee', ['fighting']), ('Hitmonchan', ['fighting']), ('Lickitung', ['normal']), ('Koffing', ['poison']), ('Weezing', ['poison']), ('Rhyhorn', ['ground', 'rock']), ('Rhydon', ['ground', 'rock']), ('Chansey', ['normal']), ('Tangela', ['grass']), ('Kangaskhan', ['normal']), ('Horsea', ['water']), ('Seadra', ['water']), ('Goldeen', ['water']), ('Seaking', ['water']), ('Staryu', ['water']), ('Starmie', ['water', 'psychic']), ('Mr. Mime', ['psychic', 'fairy']), ('Scyther', ['bug', 'flying']), ('Jynx', ['ice', 'psychic']), ('Electabuzz', ['electric']), ('Magmar', ['fire']), ('Pinsir', ['bug']), ('Tauros', ['normal']), ('Magikarp', ['water']), ('Gyarados', ['water', 'flying']), ('Lapras', ['water', 'ice']), ('Ditto', ['normal']), ('Eevee', ['normal']), ('Vaporeon', ['water']), ('Jolteon', ['electric']), ('Flareon', ['fire']), ('Porygon', ['normal']), ('Omanyte', ['rock', 'water']), ('Omastar', ['rock', 'water']), ('Kabuto', ['rock', 'water']), ('Kabutops', ['rock', 'water']), ('Aerodactyl', ['rock', 'flying']), ('Snorlax', ['normal']), ('Articuno', ['ice', 'flying']), ('Zapdos', ['electric', 'flying']), ('Moltres', ['fire', 'flying']), ('Dratini', ['dragon']), ('Dragonair', ['dragon']), ('Dragonite', ['dragon', 'flying']), ('Mewtwo', ['psychic']), ('Mew', ['psychic']), ('Chikorita', ['grass']), ('Bayleef', ['grass']), ('Meganium', ['grass']), ('Cyndaquil', ['fire']), ('Quilava', ['fire']), ('Typhlosion', ['fire']), ('Totodile', ['water']), ('Croconaw', ['water']), ('Feraligatr', ['water']), ('Sentret', ['normal']), ('Furret', ['normal']), ('Hoothoot', ['normal', 'flying']), ('Noctowl', ['normal', 'flying']), ('Ledyba', ['bug', 'flying']), ('Ledian', ['bug', 'flying']), ('Spinarak', ['bug', 'poison']), ('Ariados', ['bug', 'poison']), ('Crobat', ['poison', 'flying']), ('Chinchou', ['water', 'electric']), ('Lanturn', ['water', 'electric']), ('Pichu', ['electric']), ('Cleffa', ['fairy']), ('Igglybuff', ['normal', 'fairy']), ('Togepi', ['fairy']), ('Togetic', ['fairy', 'flying']), ('Natu', ['psychic', 'flying']), ('Xatu', ['psychic', 'flying']), ('Mareep', ['electric']), ('Flaaffy', ['electric']), ('Ampharos', ['electric']), ('Bellossom', ['grass']), ('Marill', ['water', 'fairy']), ('Azumarill', ['water', 'fairy']), ('Sudowoodo', ['rock']), ('Politoed', ['water']), ('Hoppip', ['grass', 'flying']), ('Skiploom', ['grass', 'flying']), ('Jumpluff', ['grass', 'flying']), ('Aipom', ['normal']), ('Sunkern', ['grass']), ('Sunflora', ['grass']), ('Yanma', ['bug', 'flying']), ('Wooper', ['water', 'ground']), ('Quagsire', ['water', 'ground']), ('Espeon', ['psychic']), ('Umbreon', ['dark']), ('Murkrow', ['dark', 'flying']), ('Slowking', ['water', 'psychic']), ('Misdreavus', ['ghost']), ('Unown', ['psychic']), ('Wobbuffet', ['psychic']), ('Girafarig', ['normal', 'psychic']), ('Pineco', ['bug']), ('Forretress', ['bug', 'steel']), ('Dunsparce', ['normal']), ('Gligar', ['ground', 'flying']), ('Steelix', ['steel', 'ground']), ('Snubbull', ['fairy']), ('Granbull', ['fairy']), ('Qwilfish', ['water', 'poison']), ('Scizor', ['bug', 'steel']), ('Shuckle', ['bug', 'rock']), ('Heracross', ['bug', 'fighting']), ('Sneasel', ['dark', 'ice']), ('Teddiursa', ['normal']), ('Ursaring', ['normal']), ('Slugma', ['fire']), ('Magcargo', ['fire', 'rock']), ('Swinub', ['ice', 'ground']), ('Piloswine', ['ice', 'ground']), ('Corsola', ['water', 'rock']), ('Remoraid', ['water']), ('Octillery', ['water']), ('Delibird', ['ice', 'flying']), ('Mantine', ['water', 'flying']), ('Skarmory', ['steel', 'flying']), ('Houndour', ['dark', 'fire']), ('Houndoom', ['dark', 'fire']), ('Kingdra', ['water', 'dragon']), ('Phanpy', ['ground']), ('Donphan', ['ground']), ('Porygon2', ['normal']), ('Stantler', ['normal']), ('Smeargle', ['normal']), ('Tyrogue', ['fighting']), ('Hitmontop', ['fighting']), ('Smoochum', ['ice', 'psychic']), ('Elekid', ['electric']), ('Magby', ['fire']), ('Miltank', ['normal']), ('Blissey', ['normal']), ('Raikou', ['electric']), ('Entei', ['fire']), ('Suicune', ['water']), ('Larvitar', ['rock', 'ground']), ('Pupitar', ['rock', 'ground']), ('Tyranitar', ['rock', 'dark']), ('Lugia', ['psychic', 'flying']), ('Ho-Oh', ['fire', 'flying']), ('Celebi', ['psychic', 'grass']), ('Treecko', ['grass']), ('Grovyle', ['grass']), ('Sceptile', ['grass']), ('Torchic', ['fire']), ('Combusken', ['fire', 'fighting']), ('Blaziken', ['fire', 'fighting']), ('Mudkip', ['water']), ('Marshtomp', ['water', 'ground']), ('Swampert', ['water', 'ground']), ('Poochyena', ['dark']), ('Mightyena', ['dark']), ('Zigzagoon', ['normal']), ('Linoone', ['normal']), ('Wurmple', ['bug']), ('Silcoon', ['bug']), ('Beautifly', ['bug', 'flying']), ('Cascoon', ['bug']), ('Dustox', ['bug', 'poison']), ('Lotad', ['water', 'grass']), ('Lombre', ['water', 'grass']), ('Ludicolo', ['water', 'grass']), ('Seedot', ['grass']), ('Nuzleaf', ['grass', 'dark']), ('Shiftry', ['grass', 'dark']), ('Taillow', ['normal', 'flying']), ('Swellow', ['normal', 'flying']), ('Wingull', ['water', 'flying']), ('Pelipper', ['water', 'flying']), ('Ralts', ['psychic', 'fairy']), ('Kirlia', ['psychic', 'fairy']), ('Gardevoir', ['psychic', 'fairy']), ('Surskit', ['bug', 'water']), ('Masquerain', ['bug', 'flying']), ('Shroomish', ['grass']), ('Breloom', ['grass', 'fighting']), ('Slakoth', ['normal']), ('Vigoroth', ['normal']), ('Slaking', ['normal']), ('Nincada', ['bug', 'ground']), ('Ninjask', ['bug', 'flying']), ('Shedinja', ['bug', 'ghost']), ('Whismur', ['normal']), ('Loudred', ['normal']), ('Exploud', ['normal']), ('Makuhita', ['fighting']), ('Hariyama', ['fighting']), ('Azurill', ['normal', 'fairy']), ('Nosepass', ['rock']), ('Skitty', ['normal']), ('Delcatty', ['normal']), ('Sableye', ['dark', 'ghost']), ('Mawile', ['steel', 'fairy']), ('Aron', ['steel', 'rock']), ('Lairon', ['steel', 'rock']), ('Aggron', ['steel', 'rock']), ('Meditite', ['fighting', 'psychic']), ('Medicham', ['fighting', 'psychic']), ('Electrike', ['electric']), ('Manectric', ['electric']), ('Plusle', ['electric']), ('Minun', ['electric']), ('Volbeat', ['bug']), ('Illumise', ['bug']), ('Roselia', ['grass', 'poison']), ('Gulpin', ['poison']), ('Swalot', ['poison']), ('Carvanha', ['water', 'dark']), ('Sharpedo', ['water', 'dark']), ('Wailmer', ['water']), ('Wailord', ['water']), ('Numel', ['fire', 'ground']), ('Camerupt', ['fire', 'ground']), ('Torkoal', ['fire']), ('Spoink', ['psychic']), ('Grumpig', ['psychic']), ('Spinda', ['normal']), ('Trapinch', ['ground']), ('Vibrava', ['ground', 'dragon']), ('Flygon', ['ground', 'dragon']), ('Cacnea', ['grass']), ('Cacturne', ['grass', 'dark']), ('Swablu', ['normal', 'flying']), ('Altaria', ['dragon', 'flying']), ('Zangoose', ['normal']), ('Seviper', ['poison']), ('Lunatone', ['rock', 'psychic']), ('Solrock', ['rock', 'psychic']), ('Barboach', ['water', 'ground']), ('Whiscash', ['water', 'ground']), ('Corphish', ['water']), ('Crawdaunt', ['water', 'dark']), ('Baltoy', ['ground', 'psychic']), ('Claydol', ['ground', 'psychic']), ('Lileep', ['rock', 'grass']), ('Cradily', ['rock', 'grass']), ('Anorith', ['rock', 'bug']), ('Armaldo', ['rock', 'bug']), ('Feebas', ['water']), ('Milotic', ['water']), ('Castform', ['normal']), ('Kecleon', ['normal']), ('Shuppet', ['ghost']), ('Banette', ['ghost']), ('Duskull', ['ghost']), ('Dusclops', ['ghost']), ('Tropius', ['grass', 'flying']), ('Chimecho', ['psychic']), ('Absol', ['dark']), ('Wynaut', ['psychic']), ('Snorunt', ['ice']), ('Glalie', ['ice']), ('Spheal', ['ice', 'water']), ('Sealeo', ['ice', 'water']), ('Walrein', ['ice', 'water']), ('Clamperl', ['water']), ('Huntail', ['water']), ('Gorebyss', ['water']), ('Relicanth', ['water', 'rock']), ('Luvdisc', ['water']), ('Bagon', ['dragon']), ('Shelgon', ['dragon']), ('Salamence', ['dragon', 'flying']), ('Beldum', ['steel', 'psychic']), ('Metang', ['steel', 'psychic']), ('Metagross', ['steel', 'psychic']), ('Regirock', ['rock']), ('Regice', ['ice']), ('Registeel', ['steel']), ('Latias', ['dragon', 'psychic']), ('Latios', ['dragon', 'psychic']), ('Kyogre', ['water']), ('Groudon', ['ground']), ('Rayquaza', ['dragon', 'flying']), ('Jirachi', ['steel', 'psychic']), ('Deoxys', ['psychic']), ('Turtwig', ['grass']), ('Grotle', ['grass']), ('Torterra', ['grass', 'ground']), ('Chimchar', ['fire']), ('Monferno', ['fire', 'fighting']), ('Infernape', ['fire', 'fighting']), ('Piplup', ['water']), ('Prinplup', ['water']), ('Empoleon', ['water', 'steel']), ('Starly', ['normal', 'flying']), ('Staravia', ['normal', 'flying']), ('Staraptor', ['normal', 'flying']), ('Bidoof', ['normal']), ('Bibarel', ['normal', 'water']), ('Kricketot', ['bug']), ('Kricketune', ['bug']), ('Shinx', ['electric']), ('Luxio', ['electric']), ('Luxray', ['electric']), ('Budew', ['grass', 'poison']), ('Roserade', ['grass', 'poison']), ('Cranidos', ['rock']), ('Rampardos', ['rock']), ('Shieldon', ['rock', 'steel']), ('Bastiodon', ['rock', 'steel']), ('Burmy', ['bug']), ('Wormadam', ['bug', 'grass']), ('Mothim', ['bug', 'flying']), ('Combee', ['bug', 'flying']), ('Vespiquen', ['bug', 'flying']), ('Pachirisu', ['electric']), ('Buizel', ['water']), ('Floatzel', ['water']), ('Cherubi', ['grass']), ('Cherrim', ['grass']), ('Shellos', ['water']), ('Gastrodon', ['water', 'ground']), ('Ambipom', ['normal']), ('Drifloon', ['ghost', 'flying']), ('Drifblim', ['ghost', 'flying']), ('Buneary', ['normal']), ('Lopunny', ['normal']), ('Mismagius', ['ghost']), ('Honchkrow', ['dark', 'flying']), ('Glameow', ['normal']), ('Purugly', ['normal']), ('Chingling', ['psychic']), ('Stunky', ['poison', 'dark']), ('Skuntank', ['poison', 'dark']), ('Bronzor', ['steel', 'psychic']), ('Bronzong', ['steel', 'psychic']), ('Bonsly', ['rock']), ('Mime Jr.', ['psychic', 'fairy']), ('Happiny', ['normal']), ('Chatot', ['normal', 'flying']), ('Spiritomb', ['ghost', 'dark']), ('Gible', ['dragon', 'ground']), ('Gabite', ['dragon', 'ground']), ('Garchomp', ['dragon', 'ground']), ('Munchlax', ['normal']), ('Riolu', ['fighting']), ('Lucario', ['fighting', 'steel']), ('Hippopotas', ['ground']), ('Hippowdon', ['ground']), ('Skorupi', ['poison', 'bug']), ('Drapion', ['poison', 'dark']), ('Croagunk', ['poison', 'fighting']), ('Toxicroak', ['poison', 'fighting']), ('Carnivine', ['grass']), ('Finneon', ['water']), ('Lumineon', ['water']), ('Mantyke', ['water', 'flying']), ('Snover', ['grass', 'ice']), ('Abomasnow', ['grass', 'ice']), ('Weavile', ['dark', 'ice']), ('Magnezone', ['electric', 'steel']), ('Lickilicky', ['normal']), ('Rhyperior', ['ground', 'rock']), ('Tangrowth', ['grass']), ('Electivire', ['electric']), ('Magmortar', ['fire']), ('Togekiss', ['fairy', 'flying']), ('Yanmega', ['bug', 'flying']), ('Leafeon', ['grass']), ('Glaceon', ['ice']), ('Gliscor', ['ground', 'flying']), ('Mamoswine', ['ice', 'ground']), ('Porygon-Z', ['normal']), ('Gallade', ['psychic', 'fighting']), ('Probopass', ['rock', 'steel']), ('Dusknoir', ['ghost']), ('Froslass', ['ice', 'ghost']), ('Rotom', ['electric', 'ghost']), ('Uxie', ['psychic']), ('Mesprit', ['psychic']), ('Azelf', ['psychic']), ('Dialga', ['steel', 'dragon']), ('Palkia', ['water', 'dragon']), ('Heatran', ['fire', 'steel']), ('Regigigas', ['normal']), ('Giratina', ['ghost', 'dragon']), ('Cresselia', ['psychic']), ('Phione', ['water']), ('Manaphy', ['water']), ('Darkrai', ['dark']), ('Shaymin', ['grass', 'grass']), ('Arceus', ['normal']), ('Victini', ['psychic', 'fire']), ('Snivy', ['grass']), ('Servine', ['grass']), ('Serperior', ['grass']), ('Tepig', ['fire']), ('Pignite', ['fire', 'fighting']), ('Emboar', ['fire', 'fighting']), ('Oshawott', ['water']), ('Dewott', ['water']), ('Samurott', ['water']), ('Patrat', ['normal']), ('Watchog', ['normal']), ('Lillipup', ['normal']), ('Herdier', ['normal']), ('Stoutland', ['normal']), ('Purrloin', ['dark']), ('Liepard', ['dark']), ('Pansage', ['grass']), ('Simisage', ['grass']), ('Pansear', ['fire']), ('Simisear', ['fire']), ('Panpour', ['water']), ('Simipour', ['water']), ('Munna', ['psychic']), ('Musharna', ['psychic']), ('Pidove', ['normal', 'flying']), ('Tranquill', ['normal', 'flying']), ('Unfezant', ['normal', 'flying']), ('Blitzle', ['electric']), ('Zebstrika', ['electric']), ('Roggenrola', ['rock']), ('Boldore', ['rock']), ('Gigalith', ['rock']), ('Woobat', ['psychic', 'flying']), ('Swoobat', ['psychic', 'flying']), ('Drilbur', ['ground']), ('Excadrill', ['ground', 'steel']), ('Audino', ['normal']), ('Timburr', ['fighting']), ('Gurdurr', ['fighting']), ('Conkeldurr', ['fighting']), ('Tympole', ['water']), ('Palpitoad', ['water', 'ground']), ('Seismitoad', ['water', 'ground']), ('Throh', ['fighting']), ('Sawk', ['fighting']), ('Sewaddle', ['bug', 'grass']), ('Swadloon', ['bug', 'grass']), ('Leavanny', ['bug', 'grass']), ('Venipede', ['bug', 'poison']), ('Whirlipede', ['bug', 'poison']), ('Scolipede', ['bug', 'poison']), ('Cottonee', ['grass', 'fairy']), ('Whimsicott', ['grass', 'fairy']), ('Petilil', ['grass']), ('Lilligant', ['grass']), ('Basculin', ['water']), ('Sandile', ['ground', 'dark']), ('Krokorok', ['ground', 'dark']), ('Krookodile', ['ground', 'dark']), ('Darumaka', ['fire']), ('Darmanitan', ['fire', 'fire']), ('Maractus', ['grass']), ('Dwebble', ['bug', 'rock']), ('Crustle', ['bug', 'rock']), ('Scraggy', ['dark', 'fighting']), ('Scrafty', ['dark', 'fighting']), ('Sigilyph', ['psychic', 'flying']), ('Yamask', ['ghost']), ('Cofagrigus', ['ghost']), ('Tirtouga', ['water', 'rock']), ('Carracosta', ['water', 'rock']), ('Archen', ['rock', 'flying']), ('Archeops', ['rock', 'flying']), ('Trubbish', ['poison']), ('Garbodor', ['poison']), ('Zorua', ['dark']), ('Zoroark', ['dark']), ('Minccino', ['normal']), ('Cinccino', ['normal']), ('Gothita', ['psychic']), ('Gothorita', ['psychic']), ('Gothitelle', ['psychic']), ('Solosis', ['psychic']), ('Duosion', ['psychic']), ('Reuniclus', ['psychic']), ('Ducklett', ['water', 'flying']), ('Swanna', ['water', 'flying']), ('Vanillite', ['ice']), ('Vanillish', ['ice']), ('Vanilluxe', ['ice']), ('Deerling', ['normal', 'grass']), ('Sawsbuck', ['normal', 'grass']), ('Emolga', ['electric', 'flying']), ('Karrablast', ['bug']), ('Escavalier', ['bug', 'steel']), ('Foongus', ['grass', 'poison']), ('Amoonguss', ['grass', 'poison']), ('Frillish', ['water', 'ghost']), ('Jellicent', ['water', 'ghost']), ('Alomomola', ['water']), ('Joltik', ['bug', 'electric']), ('Galvantula', ['bug', 'electric']), ('Ferroseed', ['grass', 'steel']), ('Ferrothorn', ['grass', 'steel']), ('Klink', ['steel']), ('Klang', ['steel']), ('Klinklang', ['steel']), ('Tynamo', ['electric']), ('Eelektrik', ['electric']), ('Eelektross', ['electric']), ('Elgyem', ['psychic']), ('Beheeyem', ['psychic']), ('Litwick', ['ghost', 'fire']), ('Lampent', ['ghost', 'fire']), ('Chandelure', ['ghost', 'fire']), ('Axew', ['dragon']), ('Fraxure', ['dragon']), ('Haxorus', ['dragon']), ('Cubchoo', ['ice']), ('Beartic', ['ice']), ('Cryogonal', ['ice']), ('Shelmet', ['bug']), ('Accelgor', ['bug']), ('Stunfisk', ['ground', 'electric']), ('Mienfoo', ['fighting']), ('Mienshao', ['fighting']), ('Druddigon', ['dragon']), ('Golett', ['ground', 'ghost']), ('Golurk', ['ground', 'ghost']), ('Pawniard', ['dark', 'steel']), ('Bisharp', ['dark', 'steel']), ('Bouffalant', ['normal']), ('Rufflet', ['normal', 'flying']), ('Braviary', ['normal', 'flying']), ('Vullaby', ['dark', 'flying']), ('Mandibuzz', ['dark', 'flying']), ('Heatmor', ['fire']), ('Durant', ['bug', 'steel']), ('Deino', ['dark', 'dragon']), ('Zweilous', ['dark', 'dragon']), ('Hydreigon', ['dark', 'dragon']), ('Larvesta', ['bug', 'fire']), ('Volcarona', ['bug', 'fire']), ('Cobalion', ['steel', 'fighting']), ('Terrakion', ['rock', 'fighting']), ('Virizion', ['grass', 'fighting']), ('Tornadus', ['flying']), ('Thundurus', ['electric', 'flying']), ('Reshiram', ['dragon', 'fire']), ('Zekrom', ['dragon', 'electric']), ('Landorus', ['ground', 'flying']), ('Kyurem', ['dragon', 'ice']), ('Keldeo', ['water', 'fighting']), ('Meloetta', ['normal', 'psychic']), ('Genesect', ['bug', 'steel']), ('Chespin', ['grass']), ('Quilladin', ['grass']), ('Chesnaught', ['grass', 'fighting']), ('Fennekin', ['fire']), ('Braixen', ['fire']), ('Delphox', ['fire', 'psychic']), ('Froakie', ['water']), ('Frogadier', ['water']), ('Greninja', ['water', 'dark']), ('Bunnelby', ['normal']), ('Diggersby', ['normal', 'ground']), ('Fletchling', ['normal', 'flying']), ('Fletchinder', ['fire', 'flying']), ('Talonflame', ['fire', 'flying']), ('Scatterbug', ['bug']), ('Spewpa', ['bug']), ('Vivillon', ['bug', 'flying']), ('Litleo', ['fire', 'normal']), ('Pyroar', ['fire', 'normal']), ('Flabébé', ['fairy']), ('Floette', ['fairy']), ('Florges', ['fairy']), ('Skiddo', ['grass']), ('Gogoat', ['grass']), ('Pancham', ['fighting']), ('Pangoro', ['fighting', 'dark']), ('Furfrou', ['normal']), ('Espurr', ['psychic']), ('Meowstic', ['psychic']), ('Honedge', ['steel', 'ghost']), ('Doublade', ['steel', 'ghost']), ('Aegislash', ['steel', 'ghost']), ('Spritzee', ['fairy']), ('Aromatisse', ['fairy']), ('Swirlix', ['fairy']), ('Slurpuff', ['fairy']), ('Inkay', ['dark', 'psychic']), ('Malamar', ['dark', 'psychic']), ('Binacle', ['rock', 'water']), ('Barbaracle', ['rock', 'water']), ('Skrelp', ['poison', 'water']), ('Dragalge', ['poison', 'dragon']), ('Clauncher', ['water']), ('Clawitzer', ['water']), ('Helioptile', ['electric', 'normal']), ('Heliolisk', ['electric', 'normal']), ('Tyrunt', ['rock', 'dragon']), ('Tyrantrum', ['rock', 'dragon']), ('Amaura', ['rock', 'ice']), ('Aurorus', ['rock', 'ice']), ('Sylveon', ['fairy']), ('Hawlucha', ['fighting', 'flying']), ('Dedenne', ['electric', 'fairy']), ('Carbink', ['rock', 'fairy']), ('Goomy', ['dragon']), ('Sliggoo', ['dragon']), ('Goodra', ['dragon']), ('Klefki', ['steel', 'fairy']), ('Phantump', ['ghost', 'grass']), ('Trevenant', ['ghost', 'grass']), ('Pumpkaboo', ['ghost', 'grass']), ('Gourgeist', ['ghost', 'grass']), ('Bergmite', ['ice']), ('Avalugg', ['ice']), ('Noibat', ['flying', 'dragon']), ('Noivern', ['flying', 'dragon']), ('Xerneas', ['fairy']), ('Yveltal', ['dark', 'flying']), ('Zygarde', ['dragon', 'ground']), ('Diancie', ['rock', 'fairy']), ('Hoopa', ['psychic', 'ghost']), ('Volcanion', ['fire', 'water']), ('Rowlet', ['grass', 'flying']), ('Dartrix', ['grass', 'flying']), ('Decidueye', ['grass', 'ghost']), ('Litten', ['fire']), ('Torracat', ['fire']), ('Incineroar', ['fire', 'dark']), ('Popplio', ['water']), ('Brionne', ['water']), ('Primarina', ['water', 'fairy']), ('Pikipek', ['normal', 'flying']), ('Trumbeak', ['normal', 'flying']), ('Toucannon', ['normal', 'flying']), ('Yungoos', ['normal']), ('Gumshoos', ['normal']), ('Grubbin', ['bug']), ('Charjabug', ['bug', 'electric']), ('Vikavolt', ['bug', 'electric']), ('Crabrawler', ['fighting']), ('Crabominable', ['fighting', 'ice']), ('Oricorio', ['fire', 'flying']), ('Cutiefly', ['bug', 'fairy']), ('Ribombee', ['bug', 'fairy']), ('Rockruff', ['rock']), ('Lycanroc', ['rock']), ('Wishiwashi', ['water']), ('Mareanie', ['poison', 'water']), ('Toxapex', ['poison', 'water']), ('Mudbray', ['ground']), ('Mudsdale', ['ground']), ('Dewpider', ['water', 'bug']), ('Araquanid', ['water', 'bug']), ('Fomantis', ['grass']), ('Lurantis', ['grass']), ('Morelull', ['grass', 'fairy']), ('Shiinotic', ['grass', 'fairy']), ('Salandit', ['poison', 'fire']), ('Salazzle', ['poison', 'fire']), ('Stufful', ['normal', 'fighting']), ('Bewear', ['normal', 'fighting']), ('Bounsweet', ['grass']), ('Steenee', ['grass']), ('Tsareena', ['grass']), ('Comfey', ['fairy']), ('Oranguru', ['normal', 'psychic']), ('Passimian', ['fighting']), ('Wimpod', ['bug', 'water']), ('Golisopod', ['bug', 'water']), ('Sandygast', ['ghost', 'ground']), ('Palossand', ['ghost', 'ground']), ('Pyukumuku', ['water']), ('Type: Null', ['normal']), ('Silvally', ['normal']), ('Minior', ['rock', 'flying']), ('Komala', ['normal']), ('Turtonator', ['fire', 'dragon']), ('Togedemaru', ['electric', 'steel']), ('Mimikyu', ['ghost', 'fairy']), ('Bruxish', ['water', 'psychic']), ('Drampa', ['normal', 'dragon']), ('Dhelmise', ['ghost', 'grass']), ('Jangmo-o', ['dragon']), ('Hakamo-o', ['dragon', 'fighting']), ('Kommo-o', ['dragon', 'fighting']), ('Tapu Koko', ['electric', 'fairy']), ('Tapu Lele', ['psychic', 'fairy']), ('Tapu Bulu', ['grass', 'fairy']), ('Tapu Fini', ['water', 'fairy']), ('Cosmog', ['psychic']), ('Cosmoem', ['psychic']), ('Solgaleo', ['psychic', 'steel']), ('Lunala', ['psychic', 'ghost']), ('Nihilego', ['rock', 'poison']), ('Buzzwole', ['bug', 'fighting']), ('Pheromosa', ['bug', 'fighting']), ('Xurkitree', ['electric']), ('Celesteela', ['steel', 'flying']), ('Kartana', ['grass', 'steel']), ('Guzzlord', ['dark', 'dragon']), ('Necrozma', ['psychic']), ('Magearna', ['steel', 'fairy'])])
# Please note this pokedex is a bit weird, for instance Sandshrew is ground/ice

effective_against = { "normal": [], "fighting": ["normal", "rock", "ice"], "flying": ["fighting", "bug", "grass"], "poison": ["bug", "grass"], "ground": ["poison", "rock", "fire", "electric"],
    "rock": ["flying", "bug", "fire", "ice"], "bug": ["poison", "grass", "psychic"], "ghost": ["ghost"], "fire": ["bug", "grass", "ice"], "water": ["ground", "rock", "fire"],
    "grass": ["ground", "rock", "water"], "electric": ["flying", "water"], "psychic": ["fighting", "poison"], "ice": ["flying", "ground", "grass", "dragon"], "dragon": ["dragon"],
    "steel": [], "dark" : [], "fairy": [],
}

weak_to_pokemon = {}
for pokemon, attack_types in pokedex.items():
    for attack_type in attack_types:
        for defense_type in effective_against[attack_type]:
            weak_to_pokemon.setdefault(defense_type, []).append(pokemon)
# weak_to_pokemon = {'ground': ['Bulbasaur', 'Ivysaur', 'Venusaur', 'Squirtle', ...], 'grass': ['Bulbasaur', 'Ivysaur', 'Venusaur', 'Charmander', ...], 'fighting': ['Charizard', 'Butterfree', 'Pidgey', 'Pidgeotto', ...], 'fire': ['Squirtle', 'Wartortle', 'Blastoise', 'Sandshrew', ...], 'water': ['Bulbasaur', 'Ivysaur', 'Venusaur', 'Pikachu', ...],  'normal': ['Mankey', 'Primeape', 'Poliwrath', 'Machop', ...], 'ghost': ['Gastly', 'Haunter', 'Gengar', 'Misdreavus', ...], ...}

def random_team():
    while True:
        team = []
        uncovered_types = sorted(weak_to_pokemon.items(), key=lambda p: len(p[1]))
        while uncovered_types and len(team) < 6:
            defense_type, candidates = uncovered_types[0]
            new_pokemon = random.choice(candidates)
            uncovered_types = [(defense_type, strong_pokemons) for defense_type, strong_pokemons in uncovered_types if new_pokemon not in strong_pokemons]
            team.append(new_pokemon)
        if len(team) < 6:
            team.extend(random.sample([pokemon for pokemon in pokedex if pokemon not in team], 6 - len(team)))
        if len(team) == 6:
            return team  # else erase everything and try again
print(random_team())
# ['Dusclops', 'Blaziken', 'Golem', 'Cutiefly', 'Vulpix', 'Zebstrika']

print(random_team())
# ['Honedge', 'Medicham', 'Marowak', 'Yanmega', 'Walrein', 'Shiftry']

print(random_team())
# ['Shuppet', 'Hariyama', 'Graveler', 'Spinarak', 'Kommo-o', 'Bellsprout']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM