Monday, October 29, 2018

Python: References, dictionaries and lists



I've just started writing with Python and still getting used with references and where they are and aren't used.



I've written the following code:



dummyList = self.getSprayLocation(heading, "left")
self.points['leftLeft'][0] = self.armLocations['leftX'] - self.mmToCoor(dummyList[0])

self.points['leftLeft'][1] = self.armLocations['leftY'] - self.mmToCoor(dummyList[1])
self.points['rightLeft'][0] = self.armLocations['leftX'] + self.mmToCoor(dummyList[0])
self.points['rightLeft'][1] = self.armLocations['leftY'] + self.mmToCoor(dummyList[1])
dummyList = self.getSprayLocation(heading, "mid")
print(self.points['leftLeft'][1])

self.points['leftMid'][0] = self.armLocations['midX'] - self.mmToCoor(dummyList[0])
self.points['leftMid'][1] = self.armLocations['midY'] - self.mmToCoor(dummyList[1])
self.points['rightMid'][0] = self.armLocations['midX'] + self.mmToCoor(dummyList[0])
self.points['rightMid'][1] = self.armLocations['midY'] + self.mmToCoor(dummyList[1])

print(self.points['leftLeft'][1])

dummyList = self.getSprayLocation(heading, "right")
self.points['leftRight'][0] = self.armLocations['rightX'] - self.mmToCoor(dummyList[0])
self.points['leftRight'][1] = self.armLocations['rightY'] - self.mmToCoor(dummyList[1])
self.points['rightRight'][0] = self.armLocations['rightX'] + self.mmToCoor(dummyList[0])
self.points['rightRight'][1] = self.armLocations['rightY'] + self.mmToCoor(dummyList[1])
print(self.points['leftLeft'][1])



It's in a Class, where 'points' is a dictionary containing a list:



coordinate = [0, 0]
points = {'leftLeft':coordinate, 'rightLeft':coordinate, 'leftMid':coordinate, 'rightMid':coordinate, 'leftRight':coordinate, 'rightRight':coordinate}


Note that after every block of code I print the ['leftLeft'][0] value. I expect this value not to change when I don't write to this key in the dictionary.



But when I run this code, this is the output




51.861101789
51.8611355556
51.8611192766


Which means the value is changed. In fact, all 'leftX' entries are the same and all 'rightX' entries are the same.



Now I think it has something to do with the references, but I haven't come up with a solution for this yet.



Thanks for your help!







Edit:
Thanks to JoshuaF I found that the reference was in the



coordinate = [0, 0]
points = {'leftLeft':coordinate, 'rightLeft':coordinate, 'leftMid':coordinate, 'rightMid':coordinate, 'leftRight':coordinate, 'rightRight':coordinate}



Block. 'coordinate' was the same 'coordinate' everywhere. The following fixes this:



coordinate = [0, 0]
points = {'leftLeft':coordinate[:], 'rightLeft':coordinate[:], 'leftMid':coordinate[:], 'rightMid':coordinate[:], 'leftRight':coordinate[:], 'rightRight':coordinate[:]}


I know the [:] has got something to do with references and lists. But what?







meaning of [:] in python



Python copy manual


Answer



As written, every entry in points points to the same object, coordinate. Changing any of them will change coordinate and therefore all the others.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...