Count number of times letters repeat in a text

Asked

Viewed 5,398 times

2

Can anyone give me an idea of how to write this algorithm in Python in fewer lines ? (The algorithm counts the amount of a given alphabet letter in a string).

#INSERINDO TEXTO
string=input("Digite a string no qual quer ler quais letras do alfabetos elas possui:\t")

#DICIONARIO do alfabetos
alfabetos={'a':0, 'b':0, 'c':0, 'd':0, 'e':0, 'f':0, 'g':0, 'h':0, 'i':0, 'j':0, 'k':0, 'l':0, 'm':0, 'n':0,
'o':0, 'p':0, 'q':0, 'r':0, 's':0, 't':0, 'u':0, 'v':0, 'w':0, 'x':0, 'y':0, 'z':0}

#PERCORRENDO A STRING (caractere por caractere)
for b in string:
    if ((b=='a') or (b=='A')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='b') or (b=='B')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='c') or (b=='C')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='d') or (b=='D')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='e') or (b=='E')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='f') or (b=='F')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='g') or (b=='G')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='g') or (b=='G')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='h') or (b=='H')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='i') or (b=='I')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='j') or (b=='J')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='k') or (b=='K')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='l') or (b=='L')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='m') or (b=='M')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='n') or (b=='N')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='o') or (b=='O')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='p') or (b=='P')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='q') or (b=='Q')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='r') or (b=='R')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='s') or (b=='S')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='t') or (b=='T')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='u') or (b=='U')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='v') or (b=='V')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='w') or (b=='W')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='x') or (b=='X')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='y') or (b=='Y')):
        alfabetos[b]=alfabetos[b]+1
    elif ((b=='z') or (b=='Z')):
        alfabetos[b]=alfabetos[b]+1

#VARIAVEL QUE CONTEM A CHAVES E CONTEUDO DO DICIONARIO
lista=list(alfabetos.items())

#PRINTANDO
print(lista)

3 answers

3

Yes, just use the collections.Counter, who already does it for you:

from collections import Counter

texto = 'Stack Overflow em Português'
counter = Counter(texto)

print(counter)

The exit is:

Counter({' ': 3, 't': 2, 'e': 2, 'r': 2, 'o': 2, 'u': 2, 'S': 1, 'a': 1, 'c': 1, 'k': 1, 'O': 1, 'v': 1, 'f': 1, 'l': 1, 'w': 1, 'm': 1, 'P': 1, 'g': 1, 'ê': 1, 's': 1})

In this case, it will consider all characters present in the text, including space and differentiating accented letters, e of ê, for example, and lower case letters, but you can start playing.

2

The most direct and advised way is to use what already exists, the Counter, as @Andersoncarloswoss has already indicated.

However, by taking your algorithm, you can manually do what you want without too many changes. Before I start it does not work for capital letters although they are contemplated in ifs because the dictionary does not initially have the keys uppercase and error when trying to access them.

If you want to save both versions (uppercase / lowercase) as lowercase, then just convert the whole sentence to lowercase with lower() before you go:

for b in string.lower():

You also don’t need to create the keys initially, you can create as they appear in the sentence:

#DICIONARIO do alfabetos
alfabetos = {}

#PERCORRENDO A STRING (caractere por caractere)
for b in string.lower():
    alfabetos[b] = alfabetos[b] + 1 if b in alfabetos else 1

Now in every letter that passes, if not already in the alfabetos guard with count 1 if already have guard with the count that is more 1.

From here to just interpret a-z just one more if:

#DICIONARIO do alfabetos
alfabetos = {}

#PERCORRENDO A STRING (caractere por caractere)
for letra in string.lower():
    if 'a' <= letra <= 'z':  # só se for letra
        alfabetos[letra] = alfabetos[letra] + 1 if letra in alfabetos else 1

To count the numbers you can use a defaultdict(int) which is a subclass of dict that facilitates you, because whenever it has no value will consider 0 and therefore avoids the test of existence:

from collections import defaultdict
#DICIONARIO do alfabetos
alfabetos = defaultdict(int)

#PERCORRENDO A STRING (caractere por caractere)
for letra in string.lower():
    if 'a' <= letra <= 'z':
        alfabetos[letra] += 1  # agora basta somar

Note that this last change meant adding another import

-1

for string in frase.lower():
for chave in alfabetos.keys():
    if string == chave:
        alfabetos[string] += 1

Browser other questions tagged

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