Collection-Type Letter Counter | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Collection-Type Letter Counter

I had to cheat on this test because I really don't know how to solve this despite with all the knowledge I have. Even tho I cheated and I can continue learning I still want to know how it works. Can someone please give an answer using ALL of the lessons before this project and break it down?

22nd Jun 2022, 9:18 AM
Scythe09
Scythe09 - avatar
2 Answers
+ 1
maby it helps you : text = input() letters = [] counts = [] for i in text: letters.append(i) for i in text: counts.append(text.count(i)) zipped = zip(letters, counts) print(dict(zipped))
9th Jul 2022, 11:19 PM
Andreas Paar
Andreas Paar - avatar
0
I will solve a slightly different problem to give you the opportunity to struggle with this and solve it yourself. Let's not count the characters from an input string here, but the number of times numbers appear on a list: numbers_list = [1, 1, 3, 3, 4, 5, 7, 7, 7, 7, 7, 16, 16, 31] dictionary = {} #your code goes here for number in numbers_list: if number in dictionary: dictionary[number] += 1 else: dictionary[number] = 1 print(dictionary) I see you went thourgh Python Beginner's, so you need to go through flow control (conditionals and for loops) again and try mastering them. As you see, we go through the numbers_list here (going through chars in your string will be just as easy) and check with conditional logic if a given number is already a KEY in the initially empty dictionary. If it is already a member (if), we increase the VALUE (the counter) by one; if it isn't (else), we just add this new key-value pair in the dictionary starting by the value count 1. Of course, if you adapt this exercise to count characters in your string you might as well want to check if the character in the current iteration is a blank space (' ') and ignore it (continue) using an elif clause or maybe give it its own dictionary key and count. Good luck!
25th Jun 2022, 3:17 PM
Alejandro M González
Alejandro M González - avatar