[英]Python Error: list indices must be integers or slices, not tuple
def openMap(uArr, oArr, i):
y = int(input("Row Number"))
x = int(input("Column Number"))
uArr[y,x] = oArr[y,x]
printMap(uArr)
if oArr[y,x] == "X":
return 0
else:
return 1
uArr
refers to the user array and oArr
to the original array. uArr
指的是用户数组, oArr
指的是原始数组。
I get this error:我收到此错误:
list indices must be integers or slices, not a tuple
列表索引必须是整数或切片,而不是元组
Can someone help to debug this?有人可以帮忙调试吗?
In a normal Python multidimensional list, you can't access elements as uArr[y, x]
.在普通的 Python 多维列表中,您不能以
uArr[y, x]
访问元素。 Instead use uArr[y][x]
.而是使用
uArr[y][x]
。
Perhaps you mean if oArr[y][x] == "X":
?也许你的意思是
if oArr[y][x] == "X":
? You cannot pass 2 numbers for indexing a list.您不能传递 2 个数字来索引列表。
By passing [y,x]
it means oArr[(y,x)]
And list indexing needs an integer.通过传递
[y,x]
意味着oArr[(y,x)]
并且列表索引需要一个整数。 You should do:你应该做:
if oArr[y][x] == "X":
The error-message indicates a common syntax error in Python.错误消息表示 Python 中的常见语法错误。
list indices must be integers or slices, not a tuple
列表索引必须是整数或切片,而不是元组
It is caused by using the wrong syntax when indexing a list (or array).它是由在索引列表(或数组)时使用错误的语法引起的。 What your code used as index was
x,y
was interpreted as tuple like (x,y)
.您用作索引的代码是
x,y
被解释为像(x,y)
这样的元组。
Correct would be either a single integer, like array[1]
or array[x]
or a slice like array[1:2]
to get second to third element.正确的应该是单个整数,如
array[1]
或array[x]
或像array[1:2]
这样的切片来获取第二到第三个元素。
See explained in article TypeError: list indices must be integers or slices, not str .请参阅文章TypeError 中的解释:列表索引必须是整数或切片,而不是 str 。
The indexes in any multi-dimensional array or list must be added in separate brackets.任何多维数组或列表中的索引都必须添加在单独的括号中。 So
[x][y][z]
indexes a single element in a cube or 3D-array, whereas your 2-dimensional array will just use something like [x][y]
.所以
[x][y][z]
索引立方体或 3D 数组中的单个元素,而您的二维数组将只使用[x][y]
。
To fix it, simply replace all [y,x]
by [y][x]
.要修复它,只需将所有
[y,x]
替换为[y][x]
。
def openMap(uArr, oArr, i):
y = int(input("Row Number"))
x = int(input("Column Number")) # fixed a typo
uArr[y][x] = oArr[y][x]
printMap(uArr)
if oArr[y][x] == "X":
return 0
else:
return 1
What happens if the user enters -1
or 999999999999
?如果用户输入
-1
或999999999999
会发生什么? Does your array or list allow negative indices or have a size that large?您的数组或列表是否允许负索引或具有那么大的大小?
You should check before and ask for a correct input then.您应该先检查,然后要求正确输入。
last_row = len(oArr)-1 # last index because zero-based
y = last_row + 1 # initially out-of-bounds to enter the loop
while not 0 <= y <= last_row:
y = int(input("Row Number ({}..{}): ".format(0, last_row)))
last_col = len(oArr[0])-1 # suppose it's quadratic = all rows have same length
x = last_col + 1 # initially out-of-bounds to enter the loop
while not 0 <= x <= last_col:
x = int(input("Column Number ({}..{}):".format(0, last_col)))
Note: Technically a negative index like -1
will point to the last element, -2
to the element before last, and so on.注意:从技术上讲,像
-1
这样的负索引将指向最后一个元素, -2
指向最后一个之前的元素,依此类推。
See also related questions:另见相关问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.