本文介绍了Python“列表索引必须是整数,而不是元组"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
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'.
解决方案
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
.
这篇关于Python“列表索引必须是整数,而不是元组"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!