Mar-11-2022, 11:01 AM
Good morning,
May I ask for some help with this homework please?:
My original idea was to create two lists: one with letters, the other with the number of occurence of each caracters in text.
Then to iterate on them to fill the dictionnary, called dict{}.
For the first one, I simply created a list called keys[] and added letters and signs manually (is there a better way to this?):
to count a caracter and add it the list called values I wrote:
More than the solution, I would like to understand your thinking behind it.
And please do feel free to point out my errors in the way I translated the problem, so I stop making these mistakes.
thanks,
Leyo
May I ask for some help with this homework please?:
Quote:We're given a string called text:
text=""" \ I want a hero: an uncommon want, When every year and month sends forth a new one, Till, after cloying the gazettes with cant, The age discovers he is not the true one; Of such as these I should not care to vaunt, I’ll therefore take our ancient friend Don Juan— We all have seen him, in the pantomime, Sent to the devil somewhat ere his time """We want to perform statistics by counting the number of occurrences of each letter and signs.
For this, we can use a dictionary, with the characters encountered as keys and the number of times each character is encountered as value.
We should test our code with:
print('count of "a"\t:', dict.get('a', 0))
My original idea was to create two lists: one with letters, the other with the number of occurence of each caracters in text.
Then to iterate on them to fill the dictionnary, called dict{}.
For the first one, I simply created a list called keys[] and added letters and signs manually (is there a better way to this?):
keys=['A','B','C','D','E','F','a','b','c','d','f','g']I struggle to fill the second one: I'd like to count each caracter first (potentially with a loop) then append them to a list with the .append method.
to count a caracter and add it the list called values I wrote:
counta=(sum(char == 'a' for char in text)) #define variabe counta to add occurences of 'a' to this variable values.append(counta) #append to the list valuesDoing this for all 26 letters of the alphabet, plus their capitals and the signs.. I have the feeling I should be using a loop, with an if condition. So I tried this:
count=0 for i in text: if i=='a' count=count+1 keys.append(count) if i=='b' count=count+1 keys.append(count) #but since the first condition is filled, if i=='b' doesn't workSo in brief, my question is :is there a way to fill the values list with the number of occurrences of each letter in the text, with a loop without writing 52 times the same code.
More than the solution, I would like to understand your thinking behind it.
And please do feel free to point out my errors in the way I translated the problem, so I stop making these mistakes.
thanks,
Leyo