hypothetical

say i have

for i in range(20):
   for j in range(20):
      for k in range(20):

How could I != all fo them without writing 20 statements
i !=j !=k doesnt work obviously
(I have to compare 5 words so writing a seperate for each combination would take too long.)

Recommended Answers

All 3 Replies

I have to compare 5 words so writing a seperate for each combination would take too long

I don see why you should use for loop like that for comparing 5 words.
Give an example of the words,and how you think the comparing output should look.

Problem: you can have 5 heroes on a team, output every combination possible. I'm not worried about runtime(but any efficiency increase would be nice).

lol... I need help. This code is just... like so badly written.

io = open(r"C:\Users\CookieMonster\Desktop\allcombinations.txt", "w")
heroes = "Admiral Alchemist Ancient_Apparition Anti-Mage Axe Bane_Elemental Batrider Beastmaster Bloodseeker Bone_Fletcher Bounty_Hunter Bristleback Broodmother Butcher Centaur_Warchief Chaos_Knight Clockwerk_Goblin Crystal_Maiden Dark_Seer Death_Prophet Demon_Witch Doom_Bringer Dragon_Knight Drow_Ranger Dwarven_Sniper Earthshaker Enchantress Enigma Faceless_Void Faerie_Dragon Far_Seer Geomancer Goblin_Techies Gorgon Guardian_Wisp Gyrocopter Holy_Knight Invoker Juggernaut Keeper_of_the_Light Lich Lifestealer Lightning_Revenant Lone_Druid Lord_of_Avernus Lord_of_Olympia Lycanthrope Magnataur Moon_Rider Morphling Murloc_Nightcrawler Naga_Siren Necro'lic Necrolyte Nerubian_Assassin Nerubian_Weaver Netherdrake Night_Stalker Oblivion Obsidian_Destroyer Ogre_Magi Omniknight Pandaren_Brewmaster Phantom_Assassin Phantom_Lancer Pit_Lord Priestess_of_the_Moon Prophet Queen_of_Pain Rogue_Knight Sacred_Warrior Sand_King Shadow_Demon Shadow_Fiend Shadow_Priest Shadow_Shaman Silencer Skeleton_King Slayer Slithereen_Guard Soul_Keeper Spectre Spiritbreaker Stealth_Assassin Stone_Giant Storm_Spirit Tauren_Chieftain Templar_Assassin Tidehunter Tinker Tormented_Soul Treant_Protector Troll_Warlord Twin_Head_Dragon Undying Ursa_Warrior Vengeful_Spirit Venomancer Warlock Windrunner Witch_Doctor".split()

counter = 0
for i in heroes:
    for j in heroes:
        for k in heroes:
            for l in heroes:
                for m in heroes:
                    if i != j != k != l != l: #this is where I need compare make sure none of them are the same
                        string = i + " : " + j + " : " + k + " : " + l + " : " + m
                        if string !in io:
                            io.writeline(string)
                            counter += 1

print(counter)

Thanks

With 101 heroes, there are 79 208 745 possible teams of 5. (101*100*99*98*97)/(5*4*3*2*1). Take a look at the itertools.combinations() function (which could be named 'teams' as well!).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.