0
I was doing the following exercise:
Write a program that reads a file and shows the letters in descending order of frequency. Your program should convert all entries to Low Box and only count the letters from a to z. Do not count spaces, digits, scores, or anything other than an alphabet letter. Find simple texts from many different languages and see how the frequency of letters varies between languages.
I came to this code and would like to know how I can simplify it.
arquivo= input("Insira o endereço de arquivo: ").strip('"')
texto= open(arquivo)
frase=[]
palavra= []
ordem= []
l= []
letras= {}
# Lê cada linha do texto
for line in texto:
line.rstrip()
frase= line.strip()
#Lê cada palavra da linha
for i in range(len(frase)):
palavra= frase[i].lower()
#Lê cada letra da palavra
for j in range(len(palavra)):
if palavra[j] == ' ' or palavra[j] == '\n' : continue
letras[palavra[j]]= letras.get(palavra[j], 0)+1
#Organiza tudo em uma lista de tuples
for k,v in letras.items():
l.append((v,k))
l.sort(reverse=True)
for k,v in l:
print(k,v)
Thank you so much for the tips!!
– Fabricio Frazão