Python的列表索引必须是整数,而不是元组QUOT;错误 [英] Python 'list indices must be integers, not tuple" error

查看:1947
本文介绍了Python的列表索引必须是整数,而不是元组QUOT;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作围绕着8×8二维网格房间里走动的机器人,一个部分是初始化它由机器人周围最接近的5瓦的传感器。

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)]

在这里我创建一个空的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'.

推荐答案

您说 self.room 是2D网格 - 我认为这是清单名单。在这种情况下,你应该访问它的元素为

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]

,而不是与该对索引列表外 self.x,self.y-1

这篇关于Python的列表索引必须是整数,而不是元组QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆