is that I want to count the repetitions that the word ANA has in the word BANANA. When using count, it only counts me 1, but it's two, any advice?
Explanation of code and problem: This is my code, where what I do is identify the vowels and thus be able to start all the substrings with those vowels that I extract. Then, I count the occurrences of those substrings with count and so that they don't repeat themselves, I put them in a list called formedwords_j2. Among those, the ANA substring comes out and with count it only counts 1 occurrence, but in BANANA it is repeated twice as you can see.
posicion=0
for letra in string:
if es_vocal(letra):
if not letra in vocales:
vocales.append([letra, posicion])
posicion+=1
for vocal in vocales:
palabra_j2=""
nueva_string=string[vocal[1]:]
for letra in nueva_string:
palabra_j2=palabra_j2+letra
if not palabra_j2 in palabrasFormadas_j2:
palabrasFormadas_j2.append(palabra_j2)
puntos=string.count(palabra_j2)
score_j2+=puntos
A simple solution:
The procedure is simple: we traverse the text from left to right and at each position we compare a piece against the desired word, and add 1 when counting on success.
show
produces:
I managed to solve it with this that I found on the following page: https://www.geeksforgeeks.org/python-ways-to-count-number-of-substring-in-string/