Python:TypeError:'int'对象不可下标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28696982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:37:37  来源:igfitidea点击:

Python: TypeError: 'int' object is not subscriptable

pythonlisttuplestypeerror

提问by itVico

I get a TypeError and I don't understand why. The error is at c = t[i][0](according to the debugger). I have 3 char groups(lists): g1, g2and g3and I'm trying to change the char's index by substracting the key's k1, k2or k3from the index. What I'm using now for testing:

我得到一个 TypeError,我不明白为什么。错误在c = t[i][0](根据调试器)。我有3个字符组(名单)g1g2g3我试图通过减去关键的改变字符的索引k1k2或者k3从索引。我现在用于测试的内容:

text = 'abcd'

l_text = [('a', 0), ('b', 1), ('c', 2), ('d', 3)]

k1, k2, k3 = 2, 3, 1

And this is the code:

这是代码:

def rotate_left(text, l_text, k1, k2, k3):
    i = 0
    newstr = [None]*len(text)
    for t in l_text: # t = tuple
        c = t[i][0] 
        if c in g1: # c = char
            l = int(l_text[i][1]) # l = index of the char in the list
            if l - k1 < 0:
                newstr[l%len(text)-k1] = l_text[i][0]
            else:
                newstr[l-k1] = l_text[i][0]
        elif c in g2:
            l = l_text[i][1] # l = index of the char in the list
            if l - k1 < 0:
                newstr[l%len(text)-k2] = l_text[i][0]
            else:
                newstr[l-k2] = l_text[i][0]
        else:
            l = l_text[i][1] # l = index of the char in the list
            if l - k1 < 0:
                newstr[l%len(text)-k3] = l_text[i][0]
            else:
                newstr[l-k3] = l_text[i][0]
        i += 1
    return newstr

Can someone explain me why do I get this error and how do I fix it? It's not like I'm using an inttype there. The debugger shows it's a str type and it breaks after the 2nd iteration.

有人可以解释我为什么会收到此错误以及如何修复它?这不像我在int那里使用一种类型。调试器显示它是一个 str 类型,它在第二次迭代后中断。

PS google didn't help PPS I know there is too much repetition in the code. I did it to see in the debugger what's happening.

PS google 没有帮助 PPS 我知道代码中有太多重复。我这样做是为了在调试器中查看发生了什么。

UPDATE:

更新:

Traceback (most recent call last):
  File "/hometriplerotatie.py", line 56, in <module>
    print(codeer('abcd', 2, 3, 1))
  File "/home/triplerotatie.py", line 47, in codeer
    text = rotate_left(text, l_text, k1, k2, k3)
  File "/home/triplerotatie.py", line 9, in rotate_left
    c = t[i][0] 
TypeError: 'int' object is not subscriptable

采纳答案by Martijn Pieters

You are indexing into each individualtuple:

您正在索引每个单独的元组:

c = t[i][0] 

istarts out as 0, but you increment it each loop iteration:

i开始为0,但每次循环迭代都会增加它:

i += 1

The forloop is binding tto each individual tuple from l_text, so first tis bound to ('a', 0), then to ('b', 1), etc.

for环被结合t到从每个单独的元组l_text,那么第一t势必('a', 0),然后向('b', 1)

So first you are looking at ('a', 0)[0][0]which is 'a'[0]which is 'a'. The next iteration you look at ('b', 1)[1][0]which is 1[0]which raises your exception, because integers are not sequences.

因此,首先您要查看('a', 0)[0][0]which is 'a'[0]which is 'a'。你看看下一个迭代('b', 1)[1][0]1[0]这引起了你的例外,因为整数不序列。

You need to remove the i; you do notneed to keep a running index here as the for t in l_text:is already giving you each individual tuple.

您需要删除i; 你不是要在这里保持运行指数作为for t in l_text:已经给你每个单独的元组

回答by Tom Dalton

The error is here:

错误在这里:

l_text = [('a', 0), ('b', 1), ('c', 2), ('d', 3)]

...

for t in l_text: # t = tuple
    # t is a tuple of 2 items: ('a', 0)
    c = t[i][0] # Breaks when i == 1

I think you want:

我想你想要:

c = t[0]

It doesn't break the first time round the loop because when i == 0, t[i]is 'a'and then t[i][0]is also 'a'.

它不会在第一次循环时中断,因为 when i == 0, t[i]is 'a', then t[i][0]is 'a'

回答by Tarun Gaba

You are doing the index part wrong. Your tuple is 1 dimensional, so you cant use a 2-D array subscript notation. Assuming that

你做的索引部分是错误的。您的元组是一维的,因此您不能使用二维数组下标表示法。假如说

t = ('a',0)

you should use t[0]or t[1]to access aand 0respectively.

您应该使用t[0]或分别t[1]访问a0

Hope it helps.. :)

希望能帮助到你.. :)

回答by Johan

The problem is that t is a tuple, you access the elements in a tuple that like a list. Currently you acces the elements like a 2D list which would, given your lists result in trying to indexing a char.

问题是 t 是一个元组,您可以访问元组中类似于列表的元素。当前,您可以像 2D 列表一样访问元素,鉴于您的列表会导致尝试对字符进行索引。

for t in l_text: # t = tuple
    c = t[i][0]

should be changed to

应该改为

for t in l_text: # t = tuple
    c = t[0]