Search for Python 3.xx sub-strings

0

How do I find Substring Events in a string in Python. I need to do in the hand is this my difficulty, I have to go through the string and if I find two occurrences of the same substring I have to remove one. In the output is counted the number of occurrences of the substrings and return the correct strings. The string is all lowercase and without an 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 only get with the palalvra 'indodo already with the others stays (Auula, aa, Esravatava)

    
asked by anonymous 12.04.2018 / 22:35

1 answer

2

Using regular expressions:

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

With the example above 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 at 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 example above you will have something like:

>> digite palavra: indododododo
indo

>> digite palavra: aulaulaula
aula

>> digite palavra: estavatava
estava

You can add more positions in the words list.

  

See working at repl.it

Considering 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 above example he encountered two instances of la and ula , now create the variable c and set its value as the variable a and do one condition:

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

The condition checks whether the number of words of a is greater than or equal to b , if yes, c remains equal to a otherwise c will equal b / p>

Now replace the value of c times the number of occurrences found c * str.count(c) with the value of c itself

>> 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 at repl.it , it's worth noting that it's not 100% guaranteed.

    
13.04.2018 / 08:40