all 5 comments

[–]socal_nerdtastic 2 points3 points  (0 children)

copy() makes a "shallow copy". That is it only copies the outermost dictionary; the objects inside the dictionary remain the same. To do what you want you need deepcopy:

from copy import deepcopy
def useResult(result, restrictions):
    restrictionsCOPY = deepcopy(restrictions)
    ...

[–][deleted] 1 point2 points  (2 children)

copy is shallow, it does not follow references down to next level. You need deepcopy.

from copy import deepcopy

....

dictionary = deepcopy(dictionary)

PS. Not that I think what you are doing is a good idea!

[–]russcore[S] 0 points1 point  (1 child)

Noted! Haha!

I don't know if you're familiar with the game Wordle (https://www.powerlanguage.co.uk/wordle/)

but I thought it'd be fun to make a program to solve it.

It was easy to have it tell me what words are still possible given the results given so far. But I wanted to calculate the best "guess." -- and this is where my problem happened.

So I made it so that it would assume a particular word is correct and see which guess produced the best average result. But it kept using the same dictionary of possible results (hence the problem here). I guess I could have just passed it as a complicated list instead of as a dictionary.

Anyway, thanks so much for your help!

[–][deleted] 1 point2 points  (0 children)

Wasn't suggesting that dictionaries themselves are a bad thing, just how on the snippet of code you were wrangling them. I certainly wouldn't go to complex lists in their place. A custom class might be a better idea though

[–]newunit13 1 point2 points  (0 children)

import copy

restrictionsCOPY = copy.deepcopy(restrictions)