我尝试编写一些代码,但是当我运行它时,它返回错误“TypeError:'int'对象不可订阅”:
这是代码行:
if data[key][0] == b[0] and data[key][1] == b[1]:
'data'是一个字典,其中'key'是键,'0'是值(在'和'之前)
我只需要知道为什么会发生这种情况以及如何解决这个问题。谢谢:))
代码示例
data = {
'a' : ['1','1'],
'b' : ['1','2'],
'c' : ['1','3'],
'd' : ['1','4'],
'e' : ['1','5'],
'f' : ['2','1'],
'g' : ['2','2'],
'h' : ['2','3'],
'i' : ['2','4'],
'j' : ['2','4'],
'k' : ['2','5'],
'l' : ['3','1'],
'm' : ['3','2'],
'n' : ['3','3'],
'o' : ['3','4'],
'p' : ['3','5'],
'q' : ['4','1'],
'r' : ['4','2'],
's' : ['4','3'],
't' : ['4','4'],
'u' : ['4','5'],
'v' : ['5','1'],
'w' : ['5','2'],
'x' : ['5','3'],
'y' : ['5','4'],
'z' : ['5','5']
}
total = '21333534313511424214'
count = 0
b = []
finalOuput = []
for number in total:
if count == 2:
for number in b:
for key in data:
if data[key][0] == b[0] and data[key][1] == b[1]:
finalOutput.append(b[0])
finalOutput.append(b[1])
count = 0
b = 0
total.replace(number,'')
else:
continue
else:
b.append(number)
count += 1
continue
答案 0 :(得分:0)
您已将b
从list
重新定义为int
,因此无法编入索引。
您的代码的相关摘录:
if data[key][0] == b[0] and data[key][1] == b[1]:
finalOuput.append(b[0])
finalOutput.append(b[1])
count = 0
## b has been redefined as 0 hence `b[0]` causes error ##
b = 0
total.replace(number,'')
else:
continue