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
, g2
and g3
and I'm trying to change the char's index by substracting the key's k1
, k2
or k3
from the index. What I'm using now for testing:
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 int
type there. The debugger shows it's a str type and it breaks after the 2nd iteration.
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.
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
You are indexing into each individual tuple:
c = t[i][0]
i
starts out as 0
, but you increment it each loop iteration:
i += 1
The for
loop is binding t
to each individual tuple from l_text
, so first t
is bound to ('a', 0)
, then to ('b', 1)
, etc.
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.
You need to remove the i
; you do not need to keep a running index here as the for t in l_text:
is already giving you each individual tuple.
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'
.
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 a
and 0
respectively.
Hope it helps.. :)
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With