Given a text and a letter indicate how many words appear that contain that letter

0

I was doing this exercise, however, when I run it, I do not see the number of words that contain that letter, only a zero appears, what am I wrong with? Thank you in advance

texto=raw_input("ingrese un texto: ")
letra=raw_input("ingrese una letra: ")
contador=0
textoLista=texto.split()
for palabra in textoLista:
    for caracter in palabra:
        if letra in caracter==True:
            contador=contador+1
print contador
    
asked by White Mamba 13.02.2017 в 02:56
source

2 answers

1

Using two for nested is unnecessary and very inefficient, with only one% of% we do the same but with fewer resources and more 'pythonic':

texto=raw_input("ingrese un texto: ")
letra=raw_input("ingrese una letra: ")

contador = 0
for palabra in texto.split():
    if letra in palabra:
        contador  += 1
print contador

Another solution that is somewhat more efficient and has less code is to use a generator and the preconstructed function in :

texto=raw_input("ingrese un texto: ")
letra=raw_input("ingrese una letra: ")
print(sum(letra in palabra for palabra in texto.split()))

The comparison using a text of 80000000 words (in my particular case and under Python 2.7):

  

2 cycles for equality: 49.6679999828 seconds.
  sum and generator: 16,3229999542 seconds.
  1 cycle for and in: 13.4120001793 seconds.

P.D: Your code fails by the order of evaluation of your sum sentence, your line is:

if letra in caracter==True:

It might seem that what this does is evaluate if (which returns letra in caracter or False ) and then see if the result is True . But the problem is that this is not what happens, first evaluate the equality ( True and then the == ). Let's imagine the following:

if 'a' in 'a' == True:
    print 'Si'

If we execute it, it does not print anything, the problem is that it is first done:

'a' == True

This logically is in , the rest no longer makes sense so the False returns if . To fix it we can add a parenthesis to evaluate in the order we want:

if ('a' in 'a') == True:
    print 'Si'

In your case it would be:

if (letra in caracter) == True:
    contador=contador+1

But this is redundancy, in Python it would be enough:

if letra in caracter:
    contador=contador+1

But remember that this would count all the letters equal to the one introduced in the sentence and not the words that contain the letter, the solution is to eliminate the second False as I mentioned earlier.

    
answered by 13.02.2017 / 09:48
source
1

Your last if is badly formed because you mix the syntax of for in with the syntax of if , you should compare if palabra and caracter are equal, like this:

texto=raw_input("ingrese un texto: ")
letra=raw_input("ingrese una letra: ")

contador=0
textoLista=texto.split()

for palabra in textoLista:
    for caracter in palabra:
        if letra == caracter:
            contador=contador+1
            break
print contador

Note that the line that changes is this:

if letra == caracter:

And we add a break to leave the word if we have already found the letter we were looking for in it.

Now, as you mention in the comments, if you want to avoid using a break , then you should use an auxiliary variable, like this (note the variable encontrado ):

texto=raw_input("ingrese un texto: ")
letra=raw_input("ingrese una letra: ")

contador=0
textoLista=texto.split()

for palabra in textoLista:
    encontrado = False
    for caracter in palabra:
        if (letra == caracter and encontrado == False):
            contador=contador+1
            encontrado = True

print contador

However, I do not recommend this second solution because it is slower. You have to go through all the letters of each word even if you've already found what you're looking for.

    
answered by 13.02.2017 в 03:24