In my code:
line_x = 10
c = 'A'
d = 'B'
a = {}
a['one']='| | | | | |'
a['two']='| | | | | |'
#untill thirteen
vak = a.keys()
b = a #copy the dictionary
spot = a[vak[0]]
spot2 = b[vak[0]]
a[vak[0]] = spot[:(line_x)]+ d+ spot[(line_x+1):]
b[vak[0]] = spot2[:(line_x)]+ c+ spot2[(line_x+1):]
print a[vak[0]] #output_a = | | | A | | |
print b[vak[0]] #output_b = | | | A | | | <- which should be a 'B' in the middle
I’m having some trouble with making a copy of a dictionary and changing a value.
When I want to change a value in one of the dictionaries, the value of both the original and the copy change. But I only want the original to change and have the copy to change to something else. I can’t seem to find the problem although it is in such a small and relatively easy code.
b = adoesn’t copy the dictionary, it rebindsbto point at the same object asa(so when you changea, you can observe the effect onbas well). Useb = copy.copy(a)(copymodule) instead.