[英]TypeError: 'int' object is not subscriptable
I have looked for other cases like this but none quite apply to me.... or if they do I don't understand how. 我在寻找其他类似情况,但都不适用于我....或者如果我不了解如何。
What I am trying to do is to filter through a 2D array (this): 我想做的是通过2D数组过滤(此):
blocks = [
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ],
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ]
and find out what the current part is. 并找出当前部分。 Sorry if my code is a bit sloppy, I am new to python.
抱歉,如果我的代码有点草率,我是python的新手。 This is (part) of my code,
这是我的代码的一部分
def drawBlock():
for blockL1 in blocks:
for blokL2 in blocks:
if blocks[blockL1[blockL2[1]]] == 0:
pygame.draw.rect(screen, (0, 255, 255), Rect((blockL1 * 32, blockL2 *32), (32, 32)))
pygame.draw.rect(screen, color, Rect(i * 32, ))
global blockL2
blockL2 += 1
global blockL1
blockL1 += 1
You've got a couple issues going on here. 您在这里遇到了几个问题。
The main problem being your use of the for blank in blank_holder
. 主要问题是您
for blank in blank_holder
使用for blank in blank_holder
。
When you write 当你写
for blockL1 in blocks:
its almost identical to writing 它几乎与写作相同
for ii in range(len(blocks)):
blockL1 = blocks[ii]
...
meaning that blockL1 is not an integer, or an index of blocks.. it is an element of blocks! 意思是blockL1不是整数,也不是块的索引。它是块的元素!
its value would look like: [ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ]
其值应类似于:
[ [0], [0], [0], [0], [0], [0], [0], [0], [0], [0] ]
Which is a list of lists... where as blocks
is a list of lists that contain lists... yeah 这是一个列表列表...其中包含列表的列表列表是
blocks
...是的
when you write 当你写
for blockL1 in blocks:
for blockL2 in blocks:
the value of blockL2
would be a single list [0]
blockL2
的值将是一个列表[0]
assuming you want to look at the one value contained in blockL2
, you can change: 假设您要查看
blockL2
包含的一个值,可以更改:
if blocks[blockL1[blockL2[1]]] == 0:
to: 至:
if blockL2[0] == 0: # the 1 index would go out of bounds for a list length 1
However this code looks like you need the indexed values of blockL1
and blockL2
, so I recommend you loop through lists of their indices (meaning ints
)... not their elemnents 但是,这段代码看起来像您需要
blockL1
和blockL2
的索引值,因此我建议您遍历它们的索引列表(意味着ints
)...而不是它们的元素
def drawBlock():
for blockL1 in range(len(blocks)):
for blockL2 in range(len(blockL1)):
if blocks[blockL1[blockL2[0]]] == 0:
pygame.draw.rect(screen, (0, 255, 255), Rect((blockL1 * 32, blockL2 *32), (32, 32)))
Hopefully this helps some! 希望这会有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.