I have a class that stores a list of lists in a matrix style, and it can just be indexed like [x,y].
Right now I have these set:
test_dict = {1:"cow",2:"horse"}
randommap = mapArray(None, (20,20))
random map is just filled with lists of 1s. So any index will return a 1. But here is where I get lost, maybe because of a misunderstanding about how dictionaries work:
test_dict[1]
That obviously gives back “cow”
and
randommap[1,1] #or any two x,y values up to 20 for that matter
gives me a value of 1.
But how come this gives me a key error:
test_dict[randommap[1,1]]
In isolation, indexing randommap there gives me a value of 1, so shouldn’t that 1 be supplied as an index for test_dict, thus returning me “cow”?
Update:
These are the two methods I presume are causing the issue. It appears that they are returning strings instead of integers, but I don’t know If I quite understand the difference between the two.
def __str__(self):
return str(self.mapdata)
def __repr__(self):
return str(self.mapdata)
Here is the overloaded __getitem__ method:
def __getitem__(self, (x,y)):
#Just reverses it so it works intuitively and the array is
# indexed simply like map[x,y] instead of map[y][x]
return mapArray(self.mapdata[y][x])
Sorry the formatting seems to have been messed up a bit.
Given the updated question, it seems that
__getitem__returns a new mapArray.I think your overloaded
__getitem__should be something likeinstead, (assuming that the [y][x] order is intentional).