3

I'm working on moving a robot around a 2d grid room of 8 x 8, and one part is initialising the sensors which consist of the closest 5 tiles around the robot.

self.sensors = [0 for x in xrange(5)]

here I'm creating an empty of array of 5 elements.

but when I attempt to set the value of sensors like this:

    if self.heading == 'East':
        self.sensors[0] = self.room[self.x, self.y-1]
        self.sensors[1] = self.room[self.x+1, self.y-1]
        self.sensors[2] = self.room[self.x+1, self.y]
        self.sensors[3] = self.room[self.x+1, self.y+1]
        self.sensors[4] = self.room[self.x, self.y+1]

I get the error of 'list indices must be integers, not tuples'.

Liban
  • 43
  • 1
  • 1
  • 4
  • 2
    The error is because of the way you're indexing room, not sensors. Show us how you defined room. – Cameron Feb 20 '12 at 20:19

6 Answers6

7

You say self.room is a "2d grid" -- I assume it is a list of lists. In this case, you should access its elements as

self.room[self.x][self.y-1]

instead of indexing the outer list with the pair self.x, self.y-1.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
5

The problem comes from your self.room.

Beacuse this:

self.room[self.x, self.y-1]

Is the same of:

self.room[(self.x, self.y-1)]

And that's your tuple error.

There are two possibilities:

  • self.room is a 2D array, which means that you probably meant something like:

    self.room[self.x][self.y-1]
    
  • you wanted to slice self.room:

    self.room[self.x:self.y-1]
    

Please provide more information about self.room.

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
2

self.room[self.x, self.y-1] indexes self.room with a tuple. If it is a ragged array then you must use self.room[self.x][self.y-1] instead.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Why does it give that error? I'm not passing any tuples!

Because __getitem__, which deals with [] resolution, converts self.room[1, 2] to a tuple:

class C(object):
    def __getitem__(self, k):
        return k

# Single argument is passed directly.
assert C()[0] == 0

# Multiple indices generate a tuple.
assert C()[0, 1] == (0, 1)

and lists are not made to deal with such arguments.

More examples at: https://stackoverflow.com/a/33086813/895245

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

what is the type of self.room, i think room is a list in this case you have to assign like this

if self.heading == 'East':
   self.sensors[0] = [self.x, self.y-1]

or like this

if self.heading == 'East':
    self.room = [self.x, self.y-1]
    self.sensors[0] = self.room

like this

>>> a = []
>>> type(a)
<type 'list'>

>>> a[2,3]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list indices must be integers

>>> a = [2,3]
pepo
  • 1,374
  • 11
  • 14
-2

This is because list indices must be integers, not anything else. In your case, you are trying to use tuples.

Your code is particularly odd, because there is no way you ever created self.room with tuple indices.

Marcin
  • 48,559
  • 18
  • 128
  • 201