Count trading amounts performed with python replace

Asked

Viewed 214 times

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.

2 answers

1


You can also use Regular Expressions, as I mentioned in reply given in your question Search for sub-strings Python 3.xx.

To count, do what you’re already doing, only instead of incrementing, you just change the value to 1:

import re
def corrigePalavra(str):
  count = 0
  for m in re.finditer(r"(\w+)+\1", str):
    str = str.replace(m.group(1) * str.count(m.group(1)), m.group(1), 1)
    count = 1
  return count, str

linha = 'eu estavava indodo para aaaaaa aulaula'
total = 0;
resultado = [];
for palavra in linha.split():
  count, retorno = corrigePalavra(palavra)
  total += count
  resultado.append(retorno)

print(linha)
print(' '.join(resultado))
print('{} palavra(s) corrigida(s)'.format(total))

Behold running on repl.it

1

You increment the counter but do nothing with it. One way is to return it too:

def corrigePalavra(str):
    palavra = [str[-1:], str[-2:], str[-3:], str[-4:]]
    result = str
    cont = 0
    for w in palavra:
        if result.count(w) > 1:
            result = result.replace(w * result.count(w), w, 1)
            cont += 1

    return cont, result

lista1 = 'estou indodo para a aulaula'
aux1 = lista1.split()
aux2 = []
cont_total = 0
for i in aux1:
    cont, x = corrigePalavra(i)
    cont_total += cont
    aux2.append(x)
print(aux1)
b = " ".join(aux2)
print(cont_total, b)  # 6 estou indo para a aula

However, so he tells the number of letters replaced, not the number of words affected. We can modify the program a little to solve this:

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  # Se fizermos uma substituição, marcamos palavra_modificada como True

    return palavra_modificada, result

lista1 = 'estou indodo para a aulaula'
aux1 = lista1.split()
aux2 = []
cont_palavras_modificadas = 0

for i in aux1:

    palavra_modificada, x = corrigePalavra(i)
    if palavra_modificada:
        cont_palavras_modificadas += 1

    aux2.append(x)
print(aux1)
b = " ".join(aux2)
print(cont_palavras_modificadas, b)  # 3 estou indo para a aula

Now we have the count of 3. This is because the algorithm is a bit flawed. He thinks he made a substitution on "for" because he looks at "a" and finds more than two "a" s in the word. One way to correct this is to take into account only repetitions of more than one letter:

def corrigePalavra(str):
    palavra = [str[-2:], str[-3:], str[-4:]]
...

Now the result comes out as expected.

As a broader tip, try to give more descriptive names to your variables to give more clarity to your code, especially when sharing with others. aux1 does not say anything about what the list should represent, lista1 is even a list and palavra also not a string, but a list of string fragments that are not words. Possible better names for these variables would be, for example, palavras_isoladas, frase_original, and lista_substrings.

  • Thank you pedro your tips will help me very, very thank you, I will seek to pay attention to meaningless used words.

  • Peter if I change str[-1:] I won’t be able to do in the word for example (aa) .

  • 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.

  • Excellent wmsouza, gave right your tip, thanks friend.

  • @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.

  • Hello vmsouza, friend how do I make these exchanges in a list type list1 = ['programaramar ee legalal','python ee showow','linguagemem de programacaocao']

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.