Python - A Letter from Count Dict

Write a Python function called LetterCount() that takes a string as an argument and returns a dictionary of letter values.

Line:

 print LetterCount("Abracadabra, Monsignor") 

Should produce the result:

 {'a': 5, 'c': 1, 'b': 2, 'd': 1, 'g': 1, 'i': 1, 'm': 1, 'o': 2, 'n': 2, 's': 1, 'r': 3} 

I tried:

 import collections c = collections.Counter('Abracadabra, Monsignor') print c print list(c.elements()) 

The answer I get is as follows

 {'a': 4, 'r': 3, 'b': 2, 'o': 2, 'n': 2, 'A': 1, 'c: 1, 'd': 1, 'g': 1, ' ':1, 'i':1, 'M':1 ',':1's': 1, } ['A', 'a','a','a','a','c','b','b','d','g', and so on 

Now with this import collection code c = collections.Counter ('Abracadabra, Monsignor'.lower ())

print c i get this {'a': 5, 'r': 3, 'b': 2, 'o': 2, 'n': 2, 'c: 1,' d ': 1,' g ' : 1, '': 1, 'i': 1, ',': 1's': 1,}

but the answer should be like this {'a': 5, 'c': 1, 'b': 2, 'd': 1, 'g': 1, 'i': 1, 'm': 1, 'o ': 2,' n ': 2,' s': 1, 'r': 3}

0
source share
1 answer

You are close. Please note that in the task description the case of letters is not taken into account. They want {'a': 5} , where you have {'a': 4, 'A': 1} .

So, first you need to convert the string to lowercase ( I'm sure you will learn how ).

+4
source

Source: https://habr.com/ru/post/1348109/


All Articles