0

I'm working on a program to calculate the most efficient spending of in-game currency on perks. To do this, im finding all possible combinations of perks and then making a list of the viable ones (it's not viable to spend too much currency or not spend as much as it can).

def findAllLvlCombos(powder, perkMaxLvls):
    lvlCombo = [1, 1, 1]
    lvlCombos = []
    for a in range(perkMaxLvls[0]):
        lvlCombo[0] = a + 1
        for b in range(perkMaxLvls[1]):
            lvlCombo[1] = b + 1
            for c in range(perkMaxLvls[2]):
                lvlCombo[2] = c + 1
                # see if combo is viable and append it
                validity = findComboValidity(powder, lvlCombo)
                if validity == True:
                    **lvlCombos.append(lvlCombo)**
        print('Done: ' + str(a) + '/' + str(perkMaxLvls[0]), end='\r')
    return lvlCombos

I'm having issues with the lvlCombos.append(lvlCombo) line.

If I print lvlCombo, it prints fine, and when I print lvlCombos afterwards, it prints a list with tons of the current lvlCombo. I was expecting for it to append the current lvlCombo and move on. If more info is needed, I'm happy to accomodate

  • 1
    There's only one `lvlCombo`, which you keep modifying over and over. If you want to have *different* `lvlCombo`s you need to create a new list *inside* your loop. – Samwise Feb 23 '23 at 16:37
  • Copy or deepcopy. See the linked duplicate answer, and hopefully, it should help! – Blue Robin Feb 23 '23 at 16:38
  • If you want to create a giant list of lists, it's much easier to do it via a list comprehension that creates a list on each iteration rather than repeatedly copying/mutating the same list. E.g. `return [[a, b, c] for a in range(1, perkMaxLvls[0] + 1) for b in range(1, perkMaxLvls[1] + 1) for c in range(1, perkMaxLvls[2] + 1) if findComboValidity(powder, [a, b, c])] ` – Samwise Feb 23 '23 at 16:41

0 Answers0