Python 'list indices must be integers, not tuple"错误

标签 python arrays list grid robot

我正在努力让机器人在 8 x 8 的二维网格房间内移动,一部分是初始化传感器,传感器由机器人周围最近的 5 个方 block 组成。

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

这里我创建了一个包含 5 个元素的空数组。

但是当我尝试像这样设置传感器的值时:

    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]

我收到“列表索引必须是整数,而不是元组”的错误。

最佳答案

你说 self.room 是一个“二维网格”——我假设它是一个列表列表。在这种情况下,您应该访问其元素作为

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

而不是用 self.x, self.y-1 对索引外部列表。

关于Python 'list indices must be integers, not tuple"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9367813/

相关文章:

arrays - 如何使用 JSON 文件中的数据填充数组

python - 如何将 python 列表发布到 django

python - 如何迭代不同列表的乘积?

Java 列表排序 : Is there a way to keep a list permantly sorted automatically like TreeMap?

python - 按键排序字典 - 解包错误

python - Python 的软件度量工具

python - 使用opencv按顺序检测图像中的颜色

arrays - 记录宏所做更改的最快方法

javascript - 将正则表达式匹配到字符数组

Python:多次写入文件而无需每次写入都打开/关闭