I keep having the same related error on my code and just can't figure out where is the issue. Tried all I could find online but it doesn't seems to work. Here is the bit of code. (I know it's probably not optimized but that's not the main issue here for me) also I'm on Python 3.9.1
def init(): # créer la liste de données
liste = []
liste_lettre = []
for x in range(len(texte)-1):
lettre_init = []
liste_poss = []
if texte[x][0] not in liste_lettre:
liste_lettre.append(texte[x][0])
lettre_init = texte[x][0]
for i in range(len(texte)-1):
for j in range(len(texte[i])):
if texte[i][j] == lettre_init:
if j == len(texte[i]):
liste_poss += (" ")
else:
liste_poss.append(texte[i][j+1])
print(hasattr(liste_poss, "append"))
liste_poss = lettre_init, liste_poss
liste.append(liste_poss)
return liste
ok so to explain what my code is about :
I'm trying to create a function to store the consecutive iterations of letters from a given text (texte
) into an array (liste
).
texte
is an array of words
The function checks for every letter of every word of texte
if the letter has already been verified.
If not, every time it sees the given letter (stored in lettre_init
) in a word in texte
, if this is the last letter of the word, it will add a space (" ") to the liste liste_poss
; if not the last letter of the word, it will add to liste_poss
the following letter in the word.
after verifying texte
entirely, a list consisting of the letter verified (lettre_init
) and all the following letters (liste_poss
) is added to the array liste
.
at the end, liste
is returned.
I keep having the error AttributeError: 'tuple' object has no attribute 'append'
for the line liste_poss.append(texte[i][j+1])
I verified texte
and liste_poss
with hasattr(liste_poss, "append")
and it returned True
I tried liste_poss += (texte[i][j+1])
but then the error is TypeError: can only concatenate tuple (not "str") to tuple
.
The issue seems to be related to an object being a tuple, but I just can't find what's the problem? If you have any idea please help me, I'm lost.
You create liste_poss
as a list:
liste_poss = []
But then on the last step of the outer for
loop, you reassign it to a tuple:
liste_poss = lettre_init, liste_poss
So yes, it is a tuple.