+ 7
[Python] Create a program that takes a string as input and output a dictionary representing the letter count
It's the end of module project of the intermediate python course. My code passed the first two tests but fails the last two and I don't know why. If anyone can help, here's my code: text = input() dict = {} letter_list = [] text=text.lower() for x in text: if x not in letter_list: letter_list.append(x) count=0 counts=[] for x in text: for y in text: if x == y: count+=1 text=text.replace(x,"") counts.append(count) count=0 for x in counts: if x ==0: counts.remove(x) for i in range(len(letter_list)): if letter_list[i] not in [" ","-","'","!","?"]: dict[letter_list[i]]=counts[i] print(dict)
18 Réponses
+ 18
mydict={}
for l in input():
try:
mydict[l]+=1
except:
mydict[ l]=1
+ 8
Use dictionary comprehension
# read input <text>
text = input()
# collect unique characters in a set
unique_chars = set( text )
# build a dictionary with the character as key
# and frequency of the character as value
stat = { c : text.count( c ) for c in unique_chars }
# print dictionary
print( stat )
+ 8
word = input()
d = dict.fromkeys(word, 0)
for s in word: d[s] += 1
+ 7
for letter in text:
dict[letter] = dict.get(letter, 0) + 1
This for loop will loop through the letters in your string and if the letter has not been added to the dictionary yet, it will add it and change the count to 1, otherwise it will increment the count. I think this is what youre trying to do correct?
+ 6
text = input()
dict = {}
for l in text:
if l in dict:
dict[l]+=1
else:
dict[l]=1
print(dict)
But I like Frogged and Jan Markus [PRO_crastinator] solutions better
+ 5
Bassel Selim Alamir Hanna
In that case, need to import collections module and use OrderedDict
https://code.sololearn.com/cnHJSGBD2W87/?ref=app
+ 5
Paul K Sadler I always love posts with many ways to solve a tiny challenge.
Would not like to miss yours.
+ 3
Ipang I forgot to specify that that the letters in the dictionary need to be in the order of appearance in the string.
+ 2
Jan Markus [PRO_crastinator] Counter looks like it orders the dictionary by count of occurrences, instead of by order of occurrence. Very handy, but not what Bassel Selim Alamir Hanna asked for
+ 1
a, b = input(), {}
for x in a: b[x] = a.count[x]
// Hope this executes fast
+ 1
Letter counter
create an empty dictionary
Iteriate over the characters of the string
When the characte is alphaberic, use count to count( ) how many times the character appears
then use reduce( ) to remove the character from the string
add the character to the dictionary
text = input()
dict = {}
#your code goes here
for character in text:
if character.isalpha():
dict[character] = text.count(character)
text.replace(character,"")
print(dict)
+ 1
text = str(input())
dict = {}
for letter in text :
if letter in dict:
dict[letter] +=1
else:
dict[letter] =1
print(dict)
0
text = input()
v=[b for b in text]
dict={}
for i in v:
if i in dict:
dict[i]+=1
else:
dict[i]=1
print(dict)
0
text = input() # take input
dict = {} # make an empty dictionary
for i in text: # looping from start of input string to end
if i in dict: # if letter found in dictionary then adding 1 to existing value
dict[i] += 1
else: # if letter not found then making frequency of current letter 1
dict[i] = 1
print(dict) # Printing the Dictionary
- 3
Jj
- 3
txt = str(input())
reverse = txt[::-1]
last = reverse[0]
print (last)
done...!