Can someone explain to me how the funcion list.index() functions? I have the following code:
def getPos(self,tile):
print self.tiles[5][5]
print tile
try:
myIndex = self.tiles.index(tile)
#some code
except:
print "exception raised"
#some code
The result:
<Tile.Tile instance at 0x36BCEB8>
<Tile.Tile instance at 0x36BCEB8>
exception raised
Do you have an idea why list.index() returns an exception although the tile variable is a reference to an element of tiles[][] ? Thanks a lot.
ps: btw I’m passing tiles[5][5] in this specific case
While the element does exist, it is not directly a member of
tiles:tilesis a two-dimensional list (a list of lists).tiles[5]is a list ofTiles.tiles[5][5]is a singleTile.Python does not recursively descend into a multidimensional list to find the element you’re looking for. Therefore
tiles.index(tile)fails;tiles[5].index(tile)would work.To illustrate: