i'm feeding function string, reads string char char. based on char being worked on, json template called dictionary, edited , saved final dictionary, parsed json , saved.
the problem template dictionary should stay constant, doesn't. somehow, values write intermediate variable gets saved original template dictionary, messing subsequent data i'm trying save.
am missing basic concept of dictionary? first time working dictionaries such extent wouldn't surprised.
the template dictionary:
self.map_legend = {"#": {"id": 100, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "wall", "playernumber": 0}, "-": {"id": 200, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "shield", "playernumber": 0}, "x": {"id": 300, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "alien", "playernumber": 0}, "|": {"id": 400, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "alienbullet", "playernumber": 0}, "!": {"id": 500, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "missile", "playernumber": 0}, "i": {"id": 500, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "missile", "playernumber": 1}, "m": {"id": 600, "alive": false, "x": 0, "y": 0, "width": 3, "height": 1, "type": "missilecontroller", "playernumber": 0}, "x": {"id": 700, "alive": false, "x": 0, "y": 0, "width": 3, "height": 1, "type": "alienfactory", "playernumber": 0}, "a": {"id": 800, "alive": false, "x": 0, "y": 0, "width": 3, "height": 1, "type": "ship", "playernumber": 0}, "v": {"id": 800, "alive": false, "x": 0, "y": 0, "width": 3, "height": 1, "type": "ship", "playernumber": 1}, " ": {"id": 900, "alive": false, "x": 0, "y": 0, "width": 1, "height": 1, "type": "space", "playernumber": 0}}
the problem code:
for char in self.initial_game_map: if char != "\n": element = self.map_legend[char] self.id_counters[char] += 1 element["id"] = self.id_counters[char] + element["id"] element["alive"] = true element["x"] = char_counter % self.state_json["map"]["height"] element["y"] = char_counter / self.state_json["map"]["height"] print self.map_legend[char] print element row.append(element) element = {} char_counter += 1 else: self.state_json["map"]["rows"].append(row) row = []
some output:
v {'width': 3, 'playernumber': 1, 'y': 1, 'x': 2, 'type': 'ship', 'id': 801, 'alive': true, 'height': 1} {'width': 3, 'playernumber': 1, 'y': 1, 'x': 2, 'type': 'ship', 'id': 801, 'alive': true, 'height': 1} # {'width': 1, 'playernumber': 0, 'y': 0, 'x': 18, 'type': 'wall', 'id': 103, 'alive': true, 'height': 1} {'width': 1, 'playernumber': 0, 'y': 0, 'x': 18, 'type': 'wall', 'id': 103, 'alive': true, 'height': 1}
the element
variable behaving supposed to, can see self.map_legend
assumes value of element
reason after element
changed, not want. what's going on?
element
, self.map_legend[char]
point same dictionary , if update element
update values of dictionary self.map_legend[char]
. seem want copy use:
element = self.map_legend[char].copy()
reference in python documentation: https://docs.python.org/2/library/copy.html. dictionaries shallow don't need .deepcopy()
Comments
Post a Comment