Search for sub-strings Python 3.xx

Asked

Viewed 305 times

0

How to find Substring Occurrences in a string in Python. I need to do in hand this is my difficulty, I have to go through the string and if I find two occurrences of substring equal I have to remove one. In the output is counted the number of occurrences of the substrings and return the correct strings.the string are all minuscule and without accent.

                 string= str(input('digite palavra:'))
                  for i in range(len(string)):  
                                x=string[-3:]
                                 y=string[-2:]
                                if string.find(x)and string.find(y):        
                                         a = string
                                          b = x or y
                                          for i in range(0,len(b)):
                                                   a =a.replace(b[i],"",1)
                                   Print( a)    

Example:

Aulaula - retira' ula' fica Aula
Indodo - retira 'do' fica indo
aa- retira o 'a' fica a
Estavatava- retira o 'tava' fica Estava

in my code I can only with the palalvra 'indodo already with the others stays(Auula, aa, Esravatava)

  • It seems to me you mean seek substrings in words, otherwise you would have to remove the pr of programming in practice. And by the look of it also has to be at least two letters otherwise each word could not have repeated letters. Clarify as much as possible the statement, and preferably show what you have done to try to solve it

  • Hello Isac, I am in the fight with this code, that’s right I have to look for a substring in each word, can be with 2 or 3 letters or but, in the example Aulaula has two substrings(Ula) with 3 letters, I have to delete the second to be correct.

1 answer

2

Using regular expressions:

import re
str = input('digite palavra:')
for m in re.finditer(r"\b(\w+)+\1\b", str):
  str = str.replace(m.group(1) * str.count(m.group(1)), m.group(1), 1)
print(str)

With the above example you will have something like:

>> digite palavra: indodo estavatava
indo estava

>> digite palavra: indododododo
indo

>> digite palavra: aulaulaula
aula

>> digite palavra: estavatava
estava

See working in repl it.


str = str(input('digite palavra:'))
words = [ str[-2:], str[-3:], str[-4:] ]
result = str
for w in words:
  if result.count(w) > 1:
    result = result.replace(w * result.count(w), w, 1)
print(result)

With the above example you will have something like:

>> digite palavra: indododododo
indo

>> digite palavra: aulaulaula
aula

>> digite palavra: estavatava
estava

You can add more positions in the list words.

See working in repl it.


Taking into account what we have here, you can count the word, example:

>> str = 'aulaula'
>> a = str[-3:] # = ula
>> b = str[-2:] # = la
>> print(str.count(a), str.count(b))
2 2

See that in the example above he found two occurrences of la and Ula, now create the variable c and set its value as the variable a and make a condition:

>> c = a if str.count(a) >= str.count(b) else b

The condition checks whether the amount of words a is greater than or equal to b, if yes, c remains the same as a if not c will equal b.

Now replace the value of c times the amount of occurrences found c * str.count(c) at the value of the c

>> result = str.replace(c * str.count(c), c, 1)
>> print(result)
'aula'

The complete code:

str = str(input('digite palavra:'))
a = str[-3:]
b = str[-2:]
c = a if str.count(a) >= str.count(b) else b
result = str.replace(c * str.count(c), c,1)
print(result)

See working in repl it., remember that it is not 100% guaranteed.

  • HELLO WMSOUSA, its functional code for( Aulaula and indodo), but I had forgotten to put but two words that are ( Estavatava and aa), in these the code returned them without change, I thank you for the attention.

  • See again, this second code. You can go putting the positions in words, for aa add str[-1:]

  • Hello buddy, I remade with the second code only did not work with aa, I thank you for your help

  • https://repl.it/repls/MatureWeeBetatest

  • Friend VWMSOUSA, you are a magician in programming, worked correctly your code, one day I will reach this your level to be able to help other programmers, thanks friend for the help.

Browser other questions tagged

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