Problem in counting string words using dictionaries

1

Hi, I'm working on a program where a user can put any type of words and my program brings out the different types of words and the top 5 most used words this is my code:

contador = {} 
grupo = {} 
while True :
        palabras=input("ponga palabras aqui ")
        if palabras.lower() == "x":
                break
        for linea in palabras:
          palabras = linea.split() 
        for palabra in palabras:
            palabra = palabra.lower().strip(".,") 
            if palabra not in contador:
                contador[palabra] = 1
            else:
                contador[palabra] += 1

            longitud = len(palabra)
            if longitud not in grupo:
                grupo[longitud] = { palabra } 
            else:
                grupo[longitud].add( palabra )
print("total de palabras distintat" , len(contador))

print('Las diez palabras más comunes son:')
for palabra in sorted( contador, key = contador.get, reverse=True )[:5]:
    print('\t',palabra,':',contador[palabra],'ocurrencias')

But the problem I have is that it takes only one letter: if I put a word it only takes the last letter of the word. How can I make him take the whole word?

    
asked by Microplo 30.10.2017 в 16:20
source

1 answer

1

You have a for left:

for linea in palabras:

From input you receive a string, not lines. In the first for iteras on the characters of the string. split is therefore applied on this character, not on a string. Your code is correct if you iterated over a file or a list of strings. In your case it should be something like this:

contador = {} 
grupo = {} 
while True :
        palabras=input("ponga palabras aqui ")
        if palabras.lower() == "x":
            break
        for palabra in palabras.split():
            palabra = palabra.strip(".,") 
            if palabra not in contador:
                contador[palabra] = 1
            else:
                contador[palabra] += 1

            longitud = len(palabra)
            if longitud not in grupo:
                grupo[longitud] = { palabra } 
            else:
                grupo[longitud].add( palabra )
print("Total de palabras distintas" , len(contador))

print('Las diez palabras más comunes son:')
for palabra in sorted( contador, key = contador.get, reverse=True )[:5]:
    print('\t',palabra,':',contador[palabra],'ocurrencias')

If you have no limitations as to which standard library resources to use, I recommend that you look at collections.Counter and the most_common() method, in Python 3 you can do something like this:

from collections import Counter


contador = Counter()
while True :
    palabras = input("Ponga palabras aqui: ")
    if palabras.lower() == "x":
            break
    contador.update(palabra.strip(",.").lower() for palabra in palabras.split())

print("Total de palabras distintas:",  len(contador))
print('Las diez palabras más comunes son:')
print(*('\t{}: {} ocurrencias.'.format(pal, n) for pal, n in contador.most_common(10)), sep = "\n")
    
answered by 30.10.2017 / 16:45
source