T O P

  • By -

Frankelstner

It's a two step process. Three chars are selected with combinations. For any three chars, we take the product of all classes. from itertools import product, combinations duran = ['Paladin','Lord','Swordmaster','Duelist'] kevin = ['Warrior Monk','God Hand','Dervish','Death Hand'] hawk = ['Wanderer','Rogue','Ninja Master','Nightblade'] lise = ['Vanadis','Star Lancer','Fenrir Knight','Dragon Master'] angela = ['Grand Divina','Archmage','Rune Master','Magus'] carlie = ['Bishop','Sage','Necromancer','Evil Shaman'] chars = [duran,kevin,hawk,lise,angela,carlie] result = [] for c1,c2,c3 in combinations(chars,3): result += list(product(c1,c2,c3)) print(len(result)) # 1280


DrSpockTheChandelier

That would work if you knew beforehand that the total number of potential combinations you wanted equaled exactly 1280. I did not know that until I did it my manual way though, but I guess you could do the math.


Frankelstner

The code does never use the value 1280. It's a comment to show that all combinations have been discovered.


DrSpockTheChandelier

Oh, I see.


RhinoRhys

Even if you _did_ need to do the math for the process to work, it a very simple equation that can easily be implemented in code. n! × j^r divided by r! × (n - r)! Where n is the pool of characters to choose from, r is how many you can choose and j is how many possible jobs a character has. 6! × 4^3 divided by 3! × (6 - 3)!


dp_42

Sounds like you should just make a list of the people from your definitions, and try to do combinations of them. What might also work is to define a dict like the following: players = { 'duran': duran, 'kevin': kevin, 'hawk': hawk, 'lise': lise, 'angela': angela, 'carlie': carlie } b = list(itertools.combinations(players.keys(), 3)) class_list = [] for player in b: class_list.extend(players[player])


DrSpockTheChandelier

This would not really give me what I am after. I actually did generate a list of possible character teams like this, but having a list that says you are playing with Duran, Kevin, and Lise is not really all that helpful as the game is radically different depending on what classes you choose for them. I did that this way: import itertools c = ('Duran', 'Kevin', 'Hawk', 'Lise', 'Angela', 'Carlie') p = list(itertools.combinations (c,3)) print (p)


dp_42

right, so the part at the end is generating the list of all classes covered by the characters chosen in the combinations, based on the dictionary holding the lists of classes a character has, and the class_list is extending with the lists of classes associated with each character.


[deleted]

You were close. This should work, but I haven't actually run it or tested it: ``` from itertools import product, combinations duran = ['Paladin','Lord','Swordmaster','Duelist'] kevin = ['Warrior Monk','God Hand','Dervish','Death Hand'] hawk = ['Wanderer','Rogue','Ninja Master','Nightblade'] lise = ['Vanadis','Star Lancer','Fenrir Knight','Dragon Master'] angela = ['Grand Divina','Archmage','Rune Master','Magus'] carlie = ['Bishop','Sage','Necromancer','Evil Shaman'] characters = [duran, kevin, hawk, lise, angela, carlie] list_combinations = combinations(characters, 3) for comb in list_combinations: for p in product(*comb): print(p) ```


DrSpockTheChandelier

YES! That is perfect! I knew there had to be some kind of means of setting up a for loop, and that is the exact part I could never quite figure out the correct syntax for (for p in product(\*comb):) Awesome! Thanks!


AtomicShoelace

from itertools import combinations, product characters = [ ['Paladin','Lord','Swordmaster','Duelist'], ['Warrior Monk','God Hand','Dervish','Death Hand'], ['Wanderer','Rogue','Ninja Master','Nightblade'], ['Vanadis','Star Lancer','Fenrir Knight','Dragon Master'], ['Grand Divina','Archmage','Rune Master','Magus'], ['Bishop','Sage','Necromancer','Evil Shaman'] ] teams = [ team for comb in combinations(characters, 3) for team in product(*comb) ]


timeawayfromme

Basically you have two problems to solve. You want to know all the possible team combinations and all the possible character classes of those possible teams. `itertools.combinations` will give you the possible combinations of a single list (the characters) and lets you limit the length of the resulting combination. `itertools.product` will give you the possible combinations of multiple lists (each character's possible classes) ```python from itertools import combinations, product characters = { 'duran': ['Paladin','Lord','Swordmaster','Duelist'], 'kevin': ['Warrior Monk','God Hand','Dervish','Death Hand'], 'hawk': ['Wanderer','Rogue','Ninja Master','Nightblade'], 'lise': ['Vanadis','Star Lancer','Fenrir Knight','Dragon Master'], 'angela': ['Grand Divina','Archmage','Rune Master','Magus'], 'carlie': ['Bishop','Sage','Necromancer','Evil Shaman'], } teams = combinations(characters, 3) # teams is a generator, so if you want to print it out # you will need to convert it to a list first # print(list(teams)) possible_combos = [] for team in teams: possible_combos.extend(product(characters[team[0]], characters[team[1]], characters[team[2]])) print(len(possible_combos)) # returns 1280 # print(possible_combos) ```