- 10
[SOLVED] Letter Counter Intermediate Python
I'm stuck where the dictionary will only count one value for each letter, I would appreciate being pointed in the right direction on where to go from here.. text = input() dict = {} for x in text: dict[x] = x.count(x) if x in dict: dict[x] += 1 print(dict) Edit: solved by moving my if statement to the start of the for loop 👀
22 odpowiedzi
+ 10
see this code,
I am checking that If keys or letters are avalible then just update the value by 1 , else make a key of name that letter and make it's values 1
https://code.sololearn.com/c4B7Zb7voT80/?ref=app
+ 26
Here is what i did ,it works(it would help)
text = input()
dict = {}
#your code goes here
for i in text:
if i not in dict:
dict[i]=1
else:
dict[i]+=1
print(dict)
+ 15
Bianca S You could also use dictionary comprehension here:
a = input()
print({x:a.count(x) for x in set(a)})
# Hope this helps
+ 4
Try this code
I tried it using dictionary comprehension
text = input()
dic = {I: text.count(i) for i in text }
print(dic)
+ 4
Hasnae Bouhmady your solution is the best !
+ 3
Calvin Thomas technically your code is doing what is wanted but they want in order according to order of input letters so that's why it doesn't really works.
+ 2
"""
you could also use the built-in Counter class from 'collections' module, wich return a dict-like, wich can be easily converted to a real dict:
"""
from collections import Counter
text = input()
print(dict(Counter(text)))
# https://pymotw.com/2/collections/counter.html
+ 2
What do dict={} do?
+ 2
@saad Khan
dict={} is an empty dictionary ,you need to declare it first and then you can fill in it whith keys and values
+ 2
# These three solutions for help:
#solving number 1:
text = input()
#your code goes here
from collections import Counter
c = Counter(text)
print(dict(c))
#solving number 2:
word = input('Enter word: ')
d = dict()
for i in word:
d[i] = word.count(i)
print(d)
#solving number 3:
word = input('Enter word: ')
print({letter: word.count(letter) for letter in word})
+ 1
+ 1
Here is what I did
text = input()
dict = {}
#your code goes here
for i in text:
w = 1
if i in dict:
w = text.count(i)
dict [i] = w
else:
dict[i] = w
print (dict)
+ 1
'Letter Counter'
'''Letter Counter
Given a string as input, you need to output how many times each letter appears in the string.
You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values.
Create a program to take a string as input and output a dictionary, which represents the letter count.
Sample Input
hello
Sample Output
{'h': 1, 'e': 1, 'l': 2, 'o': 1}
You need to output the dictionary object.
Note, that the letters are in the order of appearance in the string.'''
text = input()
dict1 = {}
#your code goes here
for i in text:
if i not in dict1:
dict1[i]=1
else:
dict1[i]+=1
print(dict1)
+ 1
Ahh, it's Ez guys--
#letterCounter
text = input()
dict = {}
for i in text:
dict[i] = text.count(i)
print(dict)
+ 1
text = input()
dictionary = {}
keys = dictionary.keys()
for letter in text:
if letter not in keys:
dictionary[letter] = 1
else:
dictionary[letter] = dictionary[letter] + 1
print(dictionary)
+ 1
my solution;
text = input()
dict = {}
letter=[]
for i in text:
dict.update({i:text.count(i)})
print(dict)
0
text = input()
dict = {}
for x in text:
dict[x]=text.count(x)
print(dict)
#your code goes here
0
If you want to use count you should use a function to return the dict this is for modifying code
0
My solution
text = input()
dict = {}
for x in text:
if x in dict:
dict[x]+=1
dict[x]= text.count(x)
print(dict)
0
This worked for me. Seems simple enough.
for letter in text:
dict[letter] = text.count(letter)
print(dict)