0
I need a code help, I need to count how many changes were made in the sentence, you have some idea to pass me.this code is to remove repeat substrings that are at the end of the sentence, now I need to count how many exchanges were made in the "replace" code in the list.
def corrigePalavra(str):
palavra = [str[-1:], str[-2:], str[-3:], str[-4:]]
result = str
palavra_modificada = False
for w in palavra:
if result.count(w) > 1:
result = result.replace(w * result.count(w), w, 1)
palavra_modificada = True
return palavra_modificada, result
lista1 = ['programaramar ee legalal','python ee showow','linguagemem de programacaocao']
aux2 = []
cont_palavras_modificadas = -1
for i in lista1:
aux1 = i.split()
for j in aux1:
palavra_modificada, x = corrigePalavra(j)
aux2.append(x)
if palavra_modificada:
cont_palavras_modificadas += 1
b = " ".join(aux2)
print(cont_palavras_modificadas, b)
Out of my code:
2 programar e legal
4 programar e legal python e show
6 programar e legal python e show linguagem de programacao
Saida Correta:
3 programar e legal
2 python e show
2 linguagem de programacao
namely 3 occurrences in the first sentence,2 in the second and 2 in the third.
Thank you pedro your tips will help me very, very thank you, I will seek to pay attention to meaningless used words.
– Bruno
Peter if I change str[-1:] I won’t be able to do in the word for example (aa) .
– Bruno
For some reason the correct is the initial variable
cont_palavras_modificadas
with its negative value:cont_palavras_modificadas = -1
for'estou indodo para a aulaula'
has two words to be changed and not three.– NoobSaibot
Excellent wmsouza, gave right your tip, thanks friend.
– Bruno
@wmsouza the reason is that it counts repeated letters in one word, and therefore counts
para
as a replaced word. Even though I start the variable as -1, if the phrase changes to "I’m indoctrinated to class to study" it misses again because now it counts "for" twice.– Pedro von Hertwig Batista
Hello vmsouza, friend how do I make these exchanges in a list type list1 = ['programaramar ee legalal','python ee showow','linguagemem de programacaocao']
– Bruno