I have a list of words which are also assigned a numerical value
words = [[Happy,1],[Sad,2],[Anger,3]]
I then have two additional lists, of just words and integers, produced in earlier code
List1 = [Happy,2,3,1,1,Sad,Sad,Anger]
List2 = [1,Sad,Anger,Happy,Happy,2,2,3]
I need to replace the integers, with strings from the words list. However I also need to make it so that the words in each index for List1 and List2 are not the same. I cannot use shuffle as I need to keep the order.
I was looking into 'isintance' as a way of checking for integers and replacing them, but I am unsure as to how I'd go about the random element of it.
I would base my initial attempt off of looking at the two lists a pair of items at a time. If one or both members of the pair are not strings then find a random replacement that is not equal to the other item in the pair.
That will give you something like:
Note that this updates the two lists "in place" and if you wanted to not mutate those lists we would need a slightly different set of code to build two new lists via
append()
. The logic however would be mostly the same.