Hello,
I do the following exercise:
Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages. Compare your results with the tables at wikipedia.org/wiki/Letter_frequencies.
I try my best but I am not sure if it corresponds to a required result thought there is not answer provided (in order to check it and compare if my method it´s not very different (deviated) from desired one)
The result might be like this (below) and if do so, I don´t know how to remove duplicate in "tuples". Please can you help me?
[(2, 'o'), (2, 'n'), (2, 'i'), (2, 'a'),(1, 'v'), (1, 's'), (1, 'p'),...]
My result is like this:
but we can´t see the frequencies...
What would be the most appropriate result?
Thank you very much!
Vlady
def most_frequent(s):
t=s.split()
delimiter= ''
s=delimiter.join(t)
l=list(s) #['j', 'a', 'n', 'k', 'o', 's', 'i', 'e', 'n', 'a', 'p', 'i', 'v', 'o']
f=[]
for i in l:
f.append(l.count(i)) # [1, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2]
tup=zip(f,l)
tup.sort(reverse=True)
res=[]
for freq,letter in tup:
if letter not in res:
res.append(letter)
print res # ['o', 'n', 'i', 'a', 'v', 's', 'p', 'k', 'j', 'e']
def main():
s='janko sie na pivo'
most_frequent(s)
if __name__ == '__main__':
main()