New course! Every coder should learn Generative AI!
Try a free lesson+ 2
Is my letter counter too complex?
In python exercise, we have to make a letter counter. I have seen other solutions, but is my solution too complex? Here is my code: text = input() dictionary = { 'a' : 0, 'b' : 0, 'c' : 0, 'd' : 0, 'e' : 0, 'f' : 0, 'g' : 0, 'h' : 0, 'i' : 0, 'j' : 0, 'k' : 0, 'l' : 0, 'm' : 0, 'n' : 0, 'o' : 0, 'p' : 0, 'q' : 0, 'r' : 0, 's' : 0, 't' : 0, 'u' : 0, 'v' : 0, 'w' : 0, 'x' : 0, 'y' : 0, 'z' : 0 } new_text = list(text) for i in range(len(new_text)): dictionary[new_text[i]] += 1 new_dict = dict() for j in range(len(new_text)): x = new_text[j] y = dictionary.get(new_text[j]) new_dict.update({x : y}) print(new_dict)
10 Answers
+ 20
There is a simple way
text = input()
counts = {}
for char in text:
counts[char] = counts.get(char, 0) + 1
print(counts)
+ 12
Mishu ,
we can reduce the code to some basic steps:
text = input()
letters = { } # we do not need to preset the dict
for char in text: # we can iterate directly over the input text
if char in letters: # if letter already in dict
letters[char] += 1 # increment dict value by 1
else: # if letter is not in dict
letters[char] = 1 # create new key-value pair
print(letters)
+ 7
Yes, it is complex. It is much better to use built-in data structures that provide the feature you need.
from collections import Counter
text = "She sells seashells on the seashore."
counts = Counter(text)
print(counts)
Assigning 0 to each letter is much too code with little benefit, but even that could be done more easily with the defaultdict.
And you did not even handle uppercase letters.
+ 6
# Hi, Mishu !
# Here’s another idea:
for c in sorted(set(s := input())):
print(repr(c), s.count(c))
+ 4
Hi Mishu
I think there's nothing wrong in your code
Your mind's slowly expanding to grasp the idea of loops and sets and iterations and general programming better then it did before.
Regardless of the fact of how much you already know about it.
Being said that
I think your try is correct and your logical progression is in right direction.
And as long as you can see that there can and should be a better way
To do what you did even after thinking about it for awhile.
Your mind will keep expanding with the power of excitement and curiosity it gets from this.
+ 3
Hey Mishu,
you might try this:
word = input()
print (len(list(word)))
list() converts the string into a list in which every letter is an item.
len() spits out the amount of items
EDIT: I just learned, that you actually do not even need to convert the variable into a list, as strings somewhat can be seen as a list (of letters and numbers) already. So the statement...
word = input()
print (len(word))
... gets the job done already.
I built exactly this earlier today (https://code.sololearn.com/cQ98bo2uDrQh/?ref=app)
Best wishes
+ 1
yes! A simpler way will be:-
text = input()
dic = {}
for i in text:
c=0
for j in text:
if i==j:
c+=1
dic[i]=c
print(dic)
+ 1
All you have to do is type this in on the lines provided in sololearn:
text = input()
counts = {}
for char in text:
counts[char] = counts.get(char, 0) + 1
print(counts)
0
I wish you saw mine
- 1
Yes! A simpler way will be:- text=input()
print(len(list(word)))
list()convers the string into alist in which every letter is an item.
Len()spit out the amout of items