Note that there are some explanatory texts on larger screens.

plurals
  1. POPython dictionary key error only in one specific case
    text
    copied!<p>I have a class that stores a list of lists in a matrix style, and it can just be indexed like [x,y].</p> <p>Right now I have these set:</p> <pre><code>test_dict = {1:"cow",2:"horse"} randommap = mapArray(None, (20,20)) </code></pre> <p>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:</p> <pre><code>test_dict[1] </code></pre> <p>That obviously gives back "cow" and</p> <pre><code>randommap[1,1] #or any two x,y values up to 20 for that matter </code></pre> <p>gives me a value of 1.</p> <p>But how come this gives me a key error:</p> <pre><code>test_dict[randommap[1,1]] </code></pre> <p>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"?</p> <p>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.</p> <pre><code>def __str__(self): return str(self.mapdata) def __repr__(self): return str(self.mapdata) </code></pre> <p>Here is the overloaded <code>__getitem__</code> method:</p> <pre><code>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]) </code></pre> <p>Sorry the formatting seems to have been messed up a bit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload