tl;dr: how can i delete duplicates from one list, while deleting corresponding entries in a different list?
(note that "class" here is not a class in the programming sense, but in the Dungeons and Dragons sense)
I'm trying to make a function that takes in a list of weights and returns a list of associated strings a number of times equal to the weight. I use random.choice to then select one of these items at random. Here I am removing weights due to multiple possible ways to qualify for each class (multiclassing rules in D&D).
However, I'm running into an error (IndexError: list index out of range) when I try to run this part of the code:
def makeRollable(weights, classNames):
output = []
classNames, weights2 = removeDuplicates(classNames, weights)
for i in range(0, len(weights2)):
weight = weights2[i]
for j in range(0, weight):
output.append(classNames[i])
return output
def removeDuplicates(names, weights):
for i in range(len(names)-1, 0, -1):
for j in range(len(names)-1, i, -1):
if names[i] == names[j]:
weights[i] = max(weights[i], weights[j])
del names[j]
del weights[j]
names2, weights2 = names, weights
return names2, weights2
When I run in debugger, I see that even though the names2 and weights2 have fewer entries in the removeDuplicates fuction, the weights2 list returned to the original size of weights in the makeRollable function.
while the names list has had 5 entries deleted (it was supposed to), the weights list did not. Given that I'm deleting names and weights entries at the same time, I don't see why this should be.
I thought it might be something to do with returning a variable of the same name as one passed to the function, so I made the names2 and weights2 variables to try to fix that, but it didn't seem to help. I was getting the same error with them removed.
if it's helpful, here is a sample data set (there are duplicates for the fighter, magus, and shaman classes because you can qualify for them based on different criteria, and the easiest way I could think of to check for that was to add them multiple times, and then just keep the maximum weight):
classNames = ['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Fighter', 'Mage', 'Magus', 'Magus', 'Magus', 'Monk', 'Paladin', 'Ranger', 'Rogue', 'Shaman', 'Shaman', 'Shaman', 'Sorcerer', 'Warlock', 'Wizard']
weights = [4, 2, 0, 0, 4, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 2, 2, 0]